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'
", 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
}