#How to fetch cookies from JSON file and set them in a request?

8 messages · Page 1 of 1 (latest)

analog berry
#

this format doesn't quite match http.Cookie (sameSite being a string rather than an integer and expires being a number), so you'll need to unmarshal it into your own type (preferred) or into a map and extract the values manually, set up an http.Cookie, then either use http.Request.AddCookie or set up an http.CookieJar

pulsar schooner
#

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)
        }
analog berry
#

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 🙂

pulsar schooner