Get cookie from Golang post
//Create Client
client=http.client{}
//Create Varible
var cookie []*http.Cookie
// Create Request
req, _ := http.NewRequest("GET", url, nil)
resp, err := client.Do(req) //send request
if err != nil {
return
}
cookie = resp.Cookies() //save cookies
// Create New Request
req, _ := http.NewRequest("POST", url, nil)
for i := range cookie {
req.AddCookie(cookie[i])
}
resp, err := client.Do(req) //send request
if err != nil {
return
}
To get the cookies you should call the Cookies()
method that's part of http.Response
, try this:
for _, cookie := range resp.Cookies() {
fmt.Println("Found a cookie named:", cookie.Name)
}
See the Cookie
fields here.