Search code examples
gocookiessession-cookies

Cannot load cookies into cookiejar because of invalid character


I'm trying to add a cookie to persistent storage and retrieve it in order to parse a site that needs to be logged into.

I'm getting my cookie from an extension and adding it into my cookiejar using a juju cookiejar that reads the default cookie file from env vars but I keep getting an error cannot load cookies: invalid character 'c' looking for beginning of value c-representing the first character in the txt file. I'm wondering if I'm parsing this correctly or not.

<!-- language: lang-go -->
func main(){
    jujujar, err := cookiejar.New(&cookiejar.Options{
    Filename: cookiejar.DefaultCookieFile(),
})

if err != nil {
    panic(err)
}

client := &http.Client{
    Jar: jujujar,
}

response, err := client.Get("https://example.com/categories/ProductList.aspx?Category=someCategories")

if err != nil {
    panic(err)
}

query, err := goquery.NewDocumentFromResponse(response)
if err != nil {
    panic(err)
}

myQuery := query.Find("body a").Each(func(index int, item *goquery.Selection) {
    linkTag := item
    link, _ := linkTag.Attr("href")
    linkText := linkTag.Text()
    fmt.Printf("Link #%d: '%s' - '%s'\n", index, linkText, link)
})

fmt.Print(myQuery)

}

Update, it looks like the library is looking for Json data:

// mergeFrom reads all the cookies from r and stores them in the Jar.
func (j *Jar) mergeFrom(r io.Reader) error {
decoder := json.NewDecoder(r)
// Cope with old cookiejar format by just discarding
// cookies, but still return an error if it's invalid JSON.
var data json.RawMessage
if err := decoder.Decode(&data); err != nil {
    if err == io.EOF {
        // Empty file.
        return nil
    }
    return err
}
var entries []entry
if err := json.Unmarshal(data, &entries); err != nil {
    log.Printf("warning: discarding cookies in invalid format (error: %v)", err)
    return nil
}
j.merge(entries)
return nil
}

Solution

  • Juju expects the cookies to be saved in JSON format:

    https://github.com/juju/persistent-cookiejar/blob/master/serialize.go

    The JSON needs to deserialize to []entry as defined here:

    https://github.com/juju/persistent-cookiejar/blob/master/jar.go#L140

    The plugin seems to save cookies in this format:

    [domain] / [true or false] / [true or false] / [Epoch date/time] / [name] / [content]

    Not sure what the true or false is, but you basically need to parse those lines and map them to your own Entry struct (as theirs is not exported) - you might be able to use a CSV parser with a custom delimiter, then serialize it to JSON, then use the JSON you created to pass to Juju.