Note that there are some explanatory texts on larger screens.

plurals
  1. POCookieJar does not catch incoming cookies
    primarykey
    data
    text
    <p>I'm trying to have Go submit a form on a webpage for me to simulate a login. From there I'm trying to use the cookies to keep a persistent session for one more call to a sub-page.</p> <p>I'm able to successfully do the log in with no issues, I'm just having issues catching the cookies being set by the returning server. I'm wondering if it's because their login script does several redirects? (I am getting an output).</p> <p>Any ideas why I'm not catching the cookies being returned?</p> <p>Here is the code I'm using:</p> <pre><code> import ( "crypto/tls" "fmt" "io/ioutil" "net/http" "net/url" "strings" "sync" ) type Jar struct { lk sync.Mutex cookies map[string][]*http.Cookie } var CookieJar *Jar func NewJar() *Jar { jar := new(Jar) jar.cookies = make(map[string][]*http.Cookie) return jar } // SetCookies handles the receipt of the cookies in a reply for the // given URL. It may or may not choose to save the cookies, depending // on the jar's policy and implementation. func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) { jar.lk.Lock() jar.cookies[u.Host] = cookies jar.lk.Unlock() } // Cookies returns the cookies to send in a request for the given URL. // It is up to the implementation to honor the standard cookie use // restrictions such as in RFC 6265. func (jar *Jar) Cookies(u *url.URL) []*http.Cookie { return jar.cookies[u.Host] } func NewClient() *http.Client { tr := &amp;http.Transport{ TLSClientConfig: &amp;tls.Config{InsecureSkipVerify: false}, } CookieJar = NewJar() client := &amp;http.Client{ Transport: tr, CheckRedirect: nil, Jar: CookieJar, } return client } func Login() { client := NewClient() api := "https://www.statuscake.com/App/" uri, _ := url.Parse("https://www.statuscake.com") fmt.Printf("uri: %s\n", uri) values := url.Values{} values.Add("username", username) values.Add("password", password) values.Add("Login", "yes") values.Add("redirect", "") str := values.Encode() req, err := http.NewRequest("POST", api, strings.NewReader(str)) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Accept", "text/html") req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.65 Safari/537.36") cookies := CookieJar.Cookies(uri) for i := 0; i &lt; len(cookies); i++ { fmt.Printf("Cookie[%d]: %s", i, cookies[i]) req.AddCookie(cookies[i]) } resp, err := client.Do(req) if err != nil { panic(err) } fmt.Printf("Response: %v\n", resp) fmt.Printf("Response.Cookies: %v\n", resp.Cookies()) cookies = resp.Cookies() CookieJar.SetCookies(uri, cookies) defer resp.Body.Close() if resp.StatusCode == 200 { fmt.Printf("\n\n-----\n") fmt.Println("HTTP Code: ", resp.StatusCode) fmt.Println("Response Cookies: ", resp.Cookies()) fmt.Println("Request Headers: ", req.Header) fmt.Println("Request Cookies: ", req.Cookies()) fmt.Println("Response Headers: ", resp.Header) fmt.Printf("-----\n\n") } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload