0

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.

gevageyo
  • 21
  • 2
  • Possible duplicate of [Go HTTP Post and use Cookies](https://stackoverflow.com/questions/12756782/go-http-post-and-use-cookies) – Flimzy Feb 11 '19 at 08:38
  • net/http.Request.Cookies does all you need, so what is the question? – Volker Feb 11 '19 at 08:44
  • Also: Parsing a Set-Cookie HTTP header in itself does not help much in adding to a cookie jar. – Volker Feb 11 '19 at 08:49

2 Answers2

2

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

Zak
  • 4,867
  • 15
  • 28
0

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