I am new here and a bit confused with setting cookie jar gloabally. I am using cookiejar from the http package and this is my implementation from other docs available in setting the cookies as jar in http.Client.
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
req, _ := http.NewRequest("GET", request_url, nil)
q := req.URL.Query()
q.Add("authtoken", token)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.URL.RawQuery = q.Encode()
res, _ := client.Do(req)
defer res.Body.Close()
fmt.Println(res.Cookies()) // can see the cookies here
What I am trying to achieve here is to have this declared gloabally so that once the jar is set the subsequent client request will have the cookies. If I place it in any other function it gets sets to nil again.
jar, _ := cookiejar.New(nil)
client := &http.Client{
Jar: jar,
}
Any best practise available in how to do this? Thank you.