gevageyo
gevageyo

Reputation: 21

Parsing set-cookie header in go

I want to parse cookies returned by a POST request into a cookie jar but I can't find any documentation on this. Are there some good packages or so out there? Or how do I do it?

Edit: it's not a duplicate. I am asking about parsing cookies returned by set-cookie, not sending cookies just by sending.

Upvotes: 0

Views: 2715

Answers (2)

vishal pandey
vishal pandey

Reputation: 1

Needs to be HTTP/S call to work with cookieJar, try setting the URL.Scheme to HTTP. Should work

Upvotes: 0

Zak
Zak

Reputation: 5898

It's not expected that you construct a cookie jar manually. Instead pass an http.CookieJar interface to an http.Client when making the request and the cookies will be automatically handled for you.

More information in this SO answer

Essentially you would use the http client, and the cookiejar implementation here: https://golang.org/pkg/net/http/cookiejar/

To do something like:

// All users of cookiejar should import "golang.org/x/net/publicsuffix"
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
        log.Fatal(err)
}

client := &http.Client{
        Jar: jar,
}

Example taken from here: https://golang.org/pkg/net/http/cookiejar/#example_New

Upvotes: 2

Related Questions