cookiejarparser

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2026 License: MIT Imports: 10 Imported by: 6

README

cookiejarparser

cookiejarparser is a Go library that parses a curl (netscape) cookiejar file into a Go http.CookieJar.

Usage

Assuming you have a netscape/curl style cookie jar made with something like:

$ curl -c cookies.txt -v https://github.com

That cookiejar can be used when making a web request using the following code:

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"

	"github.com/ssgelm/cookiejarparser"
)

func main() {
	cookies, err := cookiejarparser.LoadCookieJarFile("cookies.txt")
	if err != nil {
		log.Fatal(err)
	}

	client := &http.Client{
		Jar: cookies,
	}
	resp, err := client.Get("https://github.com")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()
	respData, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(respData))
}
Malformed lines

By default LoadCookieJarFile stops at the first line it cannot parse and returns an error naming that line:

incorrect number of fields in line 4.  Expected 6 or 7, got 4.

To skip malformed lines instead and keep whatever else the file contains, pass WithLenient:

cookies, err := cookiejarparser.LoadCookieJarFile("cookies.txt",
	cookiejarparser.WithLenient())

Lenient mode drops those lines without telling you. To see them, add WithMalformedLineHandler:

cookies, err := cookiejarparser.LoadCookieJarFile("cookies.txt",
	cookiejarparser.WithLenient(),
	cookiejarparser.WithMalformedLineHandler(func(lineNum int, err error) {
		log.Printf("skipping line %d: %v", lineNum, err)
	}))

Lenient mode covers lines that fail to parse. If LoadCookieJarFile cannot read the file at all it still returns an error, since that leaves the cookie jar truncated at an arbitrary point.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadCookieJarFile

func LoadCookieJarFile(path string, opts ...Option) (http.CookieJar, error)

LoadCookieJarFile takes a path to a curl (netscape) cookie jar file and crates a go http.CookieJar with the contents

By default LoadCookieJarFile stops at the first malformed line and returns an error naming it. Pass WithLenient to skip those lines instead, and WithMalformedLineHandler to see which lines it skipped.

Types

type Option added in v1.1.0

type Option func(*config)

Option configures LoadCookieJarFile.

func WithLenient added in v1.1.0

func WithLenient() Option

WithLenient causes LoadCookieJarFile to skip malformed lines rather than returning an error for them.

This covers lines that fail to parse. If LoadCookieJarFile cannot read the file at all it still returns an error, since that leaves the cookie jar truncated at an arbitrary point.

func WithMalformedLineHandler added in v1.1.0

func WithMalformedLineHandler(handler func(lineNum int, err error)) Option

WithMalformedLineHandler registers a function to call for each malformed line LoadCookieJarFile skips. Lenient mode gives no other sign that it dropped a line, so without a handler you cannot tell what the file lost. Strict mode never calls the handler, since it reports the bad line through its error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL