authenticated http client requests from golang

Okay. I have resolved this. I just needed to create a cookie jar.

I am surprised that this is not handled by default by the golang http req/client class.

The code that I had to use was:

type myjar struct {
    jar map[string] []*http.Cookie
}

func (p* myjar) SetCookies(u *url.URL, cookies []*http.Cookie) {
    fmt.Printf("The URL is : %s\n", u.String())
    fmt.Printf("The cookie being set is : %s\n", cookies)
    p.jar [u.Host] = cookies
}

func (p *myjar) Cookies(u *url.URL) []*http.Cookie {
    fmt.Printf("The URL is : %s\n", u.String())
    fmt.Printf("Cookie being returned is : %s\n", p.jar[u.Host])
    return p.jar[u.Host]
}

and then in main:

    jar := &myjar{}
    jar.jar = make(map[string] []*http.Cookie)
    client.Jar = jar

Works.


With HTTP Basic Authentication, the credentials are required for every request. Try copying the

req.SetBasicAuth("<username>", "<password>")

line before the second client.Do(req).

The reason the Firefox plugin gets the details is that the browser will cache HTTP Basic Authentication tokens for subsequent use.

Tags:

Http

Go