DEV Community


Posted on • Originally published at on


How to save cookies as a file in Golang

Go’s cookiejar doesn’t have any function to persistence cookies, so you need to do something to save cookies to a local file. To make it easy, you can use juju/persistent-cookiejar instead.

GitHub logo juju / persistent-cookiejar

cookiejar is a fork of net/http/cookiejar that allows serialisation of the stored cookies


You can use it as same as net/http/cookiejar, and can save cookies by Save.

jar, _ := cookiejar.New(nil)
http.DefaultClient.Jar = jar

// Some tasks

Enter fullscreen mode Exit fullscreen mode

Where to save is decided by an option you put into New; it is set to $GOCOOKIES or $HOME/.go-cookies by default. If the file doesn't exist, it returns no errors and creates it when saving.

jar, _ := cookiejar.New(&cookiejar.Options{Filename: "path/to/cookie"})
Enter fullscreen mode Exit fullscreen mode

Try to use

This is a sample program to log in to AtCoder. It outputs Already logged in! if you've already logged in, or asks your username and password if not yet.

Some error handlings were omitted.
package main

import (


func main() {
    jar, _ := cookiejar.New(nil)
    http.DefaultClient.Jar = jar
    defer jar.Save()

    // If the file exists, check whether logged in
    _, err := os.Stat(cookiejar.DefaultCookieFile())
    if err == nil {
        doc, _ := getDocument("")
        navbarRight := doc.Find("div#navbar-collapse > ul.navbar-right")
        if navbarRight.Children().Length() == 2 {
            fmt.Println("Already logged in!")

    var username string
    fmt.Print("Username: ")

    fmt.Print("Password: ")
    // Don't press ctrl+c while Go reads password because your terminal won't display any inputs after that.
    // To resolve it, see
    bypePassword, _ := term.ReadPassword(int(os.Stdin.Fd()))

    loginUrl := ""
    doc, _ := getDocument(loginUrl)
    token, found := doc.Find(`form input[type="hidden"]`).Attr("value")
    if !found {
        log.Fatal("error: cannot find CSRF token")

    values := url.Values{
        "username":   {username},
        "password":   {string(bypePassword)},
        "csrf_token": {token},
    req, _ := http.NewRequest("POST", loginUrl, strings.NewReader(values.Encode()))
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
    defer resp.Body.Close()

    if resp.Request.URL.String() == loginUrl {
        log.Fatal("Failed to login. Check your username/password")

    fmt.Println("Successfully logged in!")

func getDocument(url string) (*goquery.Document, error) {
    resp, err := http.Get(url)
    if err != nil {
    defer resp.Body.Close()

    return goquery.NewDocumentFromReader(resp.Body)

Now you can find the cookie as a file.

$ cat ~/.go-cookies
Enter fullscreen mode Exit fullscreen mode

If you execute it again when the file exists,

$ go run main.go
Already logged in!
Enter fullscreen mode Exit fullscreen mode

it won't ask you username/password.

Top comments (0)