I have the below code that works as intended, however upon the first request, I have the cookies I want, and just want to change a value of 1 cookie before sending another request. So far I have been rather unsuccessful.
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatal(err)
}
client = &http.Client{
Jar: jar,
}
firstRequest() // aka login
mainLinkedinURL := "http://www.example.com/"
cookieURL, _ := url.Parse(mainLinkedinURL)
for j, i := range jar.Cookies(cookieURL) {
if i.Name == "JSESSIONID" {
jar.Cookies(cookieURL)[j].Value = "Another New Value"
i.Value = "Another way of setting a new value"
}
}
secondRequest() // request after changing cookie
This is only from the http.Client perspective, and no server-side cookie management.
To change value of cookies you need to use SetCookies method.
Because you need just to change a single cookie value you need something like this:
mainLinkedinURL := "http://www.example.com/"
cookieURL, err := url.Parse(mainLinkedinURL)
if err != nil {
// handle error properly
}
cookies := jar.Cookies(cookieURL)
for _, cookie := range cookies {
// note: if there is no cookie with such a name
// then a new value would not appear, please pay attention to this
if cookie.Name == "JSESSIONID" {
cookie.Value = "Another New Value"
break
}
}
jar.SetCookies(cookieURL, cookies)