https://github.com/nhatthm/go-cookiejar
Persistent Cookie Jar for HTTP Client
https://github.com/nhatthm/go-cookiejar
afero aferofs cookie cookiejar go golang http http-client http-cookie http-cookies
Last synced: 8 months ago
JSON representation
Persistent Cookie Jar for HTTP Client
- Host: GitHub
- URL: https://github.com/nhatthm/go-cookiejar
- Owner: nhatthm
- License: bsd-3-clause
- Created: 2023-02-22T14:46:18.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2025-04-22T07:19:26.000Z (11 months ago)
- Last Synced: 2025-05-07T17:05:24.294Z (11 months ago)
- Topics: afero, aferofs, cookie, cookiejar, go, golang, http, http-client, http-cookie, http-cookies
- Language: Go
- Homepage:
- Size: 84 KB
- Stars: 17
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cookiejar
[](https://github.com/nhatthm/go-cookiejar/releases/latest)
[](https://github.com/nhatthm/go-cookiejar/actions/workflows/test.yaml)
[](https://codecov.io/gh/nhatthm/go-cookiejar)
[](https://goreportcard.com/report/go.nhat.io/cookiejar)
[](https://pkg.go.dev/go.nhat.io/cookiejar)
[](https://www.paypal.com/donate/?hosted_button_id=PJZSGJN57TDJY)
The Persistent Cookiejar is a fork of [`net/http/cookiejar`](https://pkg.go.dev/net/http/cookiejar) which also implements methods for persisting the cookies to
a filesystem and retrieving them using [`spf13/afero`](https://github.com/spf13/afero)
## Prerequisites
- `Go >= 1.23`
## Install
```bash
go get go.nhat.io/cookiejar
```
## Usage
Construct the cookiejar with the following options:
| Option | Description | Default Value |
|:-----------------------|:------------------------------------------------------------------------------------------------------------------------------------|:-----------------:|
| `WithFilePath` | The path to the file to store the cookies | `"cookies.json"` |
| `WithFilePerm` | The file permission to use for persisting the cookies | `0600` |
| `WithAutoSync` | Whether to automatically sync the cookies to the file after each request | `false` |
| `WithLogger` | The logger to use for logging | No log |
| `WithFs` | The filesystem to use for persisting the cookies | `afero.NewOsFs()` |
| `WithSerDer` | The serializer/deserializer to use for persisting the cookies | `json` |
| `WithPublicSuffixList` | The public suffix list to use for cookie domain matching All users of cookiejar should import `golang.org/x/net/publicsuffix` | `nil` |
Example:
```go
package example
import (
"net/http"
"go.nhat.io/cookiejar"
)
func newClient() *http.Client {
jar := cookiejar.NewPersistentJar(
cookiejar.WithFilePath("/path/to/cookies.json"),
cookiejar.WithFilePerm(0755),
cookiejar.WithAutoSync(true),
)
return &http.Client{
Jar: jar,
}
}
```
## Examples
```go
package cookiejar_test
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"go.nhat.io/cookiejar"
)
func ExampleNewPersistentJar() {
tempDir, err := os.MkdirTemp(os.TempDir(), "example")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
cookiesFile := filepath.Join(tempDir, "cookies")
// Start a server to give us cookies.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if cookie, err := r.Cookie("Flavor"); err != nil {
http.SetCookie(w, &http.Cookie{Name: "Flavor", Value: "Chocolate Chip"})
} else {
cookie.Value = "Oatmeal Raisin"
http.SetCookie(w, cookie)
}
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
log.Fatal(err)
}
jar := cookiejar.NewPersistentJar(
cookiejar.WithFilePath(cookiesFile),
cookiejar.WithAutoSync(true),
// All users of cookiejar should import "golang.org/x/net/publicsuffix"
cookiejar.WithPublicSuffixList(publicsuffix.List),
)
client := &http.Client{
Jar: jar,
}
if _, err = client.Get(u.String()); err != nil {
log.Fatal(err)
}
fmt.Println("After 1st request:")
for _, cookie := range jar.Cookies(u) {
fmt.Printf(" %s: %s\n", cookie.Name, cookie.Value)
}
if _, err = client.Get(u.String()); err != nil {
log.Fatal(err)
}
fmt.Println("After 2nd request:")
for _, cookie := range jar.Cookies(u) {
fmt.Printf(" %s: %s\n", cookie.Name, cookie.Value)
}
// Output:
// After 1st request:
// Flavor: Chocolate Chip
// After 2nd request:
// Flavor: Oatmeal Raisin
}
```
## Donation
If this project help you reduce time to develop, you can give me a cup of coffee :)
### Paypal donation
[](https://www.paypal.com/donate/?hosted_button_id=PJZSGJN57TDJY)
or scan this