net/http/cookiejar

Guided tour · Networking · pkg.go.dev →

An in-memory HTTP cookie store that implements http.CookieJar. Stick it on an http.Client to persist cookies across requests.

Enable cookies on a client

Log in once, reuse the session

jar, _ := cookiejar.New(nil)
client := &http.Client{Jar: jar}

client.PostForm("https://example.com/login", url.Values{
    "user": {"ada"}, "password": {"hunter2"},
})
client.Get("https://example.com/account")   // cookies tagged automatically

Public suffix list for security

Pass a PublicSuffixList in the Options so cookies can't be set for 'co.uk' and similar.