#How to fetch cookies from JSON file and set them in a request?
8 messages · Page 1 of 1 (latest)
Thanks! That seems to compile just fine, however, the API now tells me that my request couldn't be processed. Previously, it just told me that I need to login first (hence the need for cookies). Commenting this code out seems to bring that response back, so I'm not sure if I'm setting the cookies right
Here's my code
file, err := os.Open("cookies.json")
if err != nil {
log.Fatalf("could not open file: %v", err)
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
log.Fatalf("could not read data: %v", err)
}
var cookies []CookieData
if err := json.Unmarshal(data, &cookies); err != nil {
log.Fatalf("could not unmarshal data: %v", err)
}
var httpCookies []*http.Cookie
for _, data := range cookies {
cookie := &http.Cookie{
Name: data.Name,
Value: data.Value,
Path: data.Path,
Domain: data.Domain,
Secure: data.Secure,
HttpOnly: data.HttpOnly,
}
httpCookies = append(httpCookies, cookie)
}
for _, cookie := range httpCookies {
req.AddCookie(cookie)
}
you may find it helpful to use httputil.DumpRequestOut to see (approximately) what's going out over the wire
i say approximately because it converts HTTP/2 into a text-based format that's not actually what gets sent, but it (ideally) retains the same semantics
wireshark is also a classic tool, and you can use KeyLogWriter on tls.Config to dump TLS keys if your requests are encrypted
i could throw out ideas of what's going on, but i think seeing what the client is actually doing is going to be far more valuable 🙂
I have fixed it by just making it copy the cookie headers that my Playwright browser is using (since I'm using both these modules in conjunction)