Skip to content

Instantly share code, notes, and snippets.

@adilsoncarvalho
Created October 13, 2024 01:09
Show Gist options
  • Save adilsoncarvalho/406395f1724510b56b3423e0133b2d7e to your computer and use it in GitHub Desktop.
Save adilsoncarvalho/406395f1724510b56b3423e0133b2d7e to your computer and use it in GitHub Desktop.
Go net/http/CookieJar
package main
import (
"log/slog"
"net/http"
"net/http/cookiejar"
"os"
)
func main() {
jar, err := cookiejar.New(nil)
if err != nil {
slog.Error("Could not create cookie jar", "Error", err)
os.Exit(1)
}
client := http.Client{
Transport: nil,
CheckRedirect: nil,
Jar: jar,
Timeout: 0,
}
//
// First request
// - client.Jar.Cookies() must be empty
// - request.Cookies() must be empty
//
slog.Info("First Request - no cookies in the cookie jar / no cookies sent to server")
slog.Info("CookieJar must be empty", "CookieJar", client.Jar)
get, err := client.Get("http://localhost:8080/")
slog.Info("Cookies sent with request", "Cookies", get.Request.Cookies())
if err != nil {
panic(err)
}
//
// Second request
// - client.Jar.Cookies() must have one item
// - request.Cookies() must have one item
//
slog.Info("Second Request - one cookie in the cookie jar / one cookie sent to server")
slog.Info("CookieJar must have one cookie", "CookieJar", client.Jar)
get, err = client.Get("http://localhost:8080/")
slog.Info("Cookies sent with request", "Cookies", get.Request.Cookies())
}
2024/10/13 12:05:15 INFO First Request - no cookies in the cookie jar / no cookies sent to server
2024/10/13 12:05:15 INFO CookieJar must be empty CookieJar="&{psList:<nil> mu:{state:0 sema:0} entries:map[] nextSeqNum:0}"
2024/10/13 12:05:15 INFO Cookies sent with request Cookies=[]
2024/10/13 12:05:15 INFO Second Request - one cookie in the cookie jar / one cookie sent to server
2024/10/13 12:05:15 INFO CookieJar must have one cookie CookieJar="&{psList:<nil> mu:{state:0 sema:0} entries:map[localhost:map[localhost;/;JSESSIONID:{Name:JSESSIONID Value:ABSCDEDASDSSDSSE.oai007 Domain:localhost Path:/ SameSite: Secure:false HttpOnly:true Persistent:false HostOnly:true Expires:{wall:0 ext:315537897599 loc:<nil>} Creation:{wall:13959721864439615168 ext:6148372 loc:0x3fe35a0} LastAccess:{wall:13959721864439615168 ext:6148372 loc:0x3fe35a0} seqNum:0}]] nextSeqNum:1}"
2024/10/13 12:05:15 INFO Cookies sent with request Cookies="[JSESSIONID=ABSCDEDASDSSDSSE.oai007]"
Process finished with the exit code 0
{
"mappings": [
{
"request": {
"method": "GET",
"urlPath": "/"
},
"response": {
"status": 200,
"body": "SUCCESS",
"headers": {
"Set-Cookie": [
"JSESSIONID=ABSCDEDASDSSDSSE.oai007; path=/; HttpOnly"
]
}
}
}
]
}
2024-10-13 12:05:15.757 Request received:
[0:0:0:0:0:0:0:1] - GET /
Host: [localhost:8080]
User-Agent: [Go-http-client/1.1]
Accept-Encoding: [gzip]
Matched response definition:
{
"status" : 200,
"body" : "SUCCESS",
"headers" : {
"Set-Cookie" : "JSESSIONID=ABSCDEDASDSSDSSE.oai007; path=/; HttpOnly"
}
}
Response:
HTTP/1.1 200
Set-Cookie: [JSESSIONID=ABSCDEDASDSSDSSE.oai007; path=/; HttpOnly]
Matched-Stub-Id: [e4fbb9f6-44b9-46fb-9294-e4852a84e201]
2024-10-13 12:05:15.760 Request received:
[0:0:0:0:0:0:0:1] - GET /
Host: [localhost:8080]
User-Agent: [Go-http-client/1.1]
Cookie: [JSESSIONID=ABSCDEDASDSSDSSE.oai007]
Accept-Encoding: [gzip]
Matched response definition:
{
"status" : 200,
"body" : "SUCCESS",
"headers" : {
"Set-Cookie" : "JSESSIONID=ABSCDEDASDSSDSSE.oai007; path=/; HttpOnly"
}
}
Response:
HTTP/1.1 200
Set-Cookie: [JSESSIONID=ABSCDEDASDSSDSSE.oai007; path=/; HttpOnly]
Matched-Stub-Id: [e4fbb9f6-44b9-46fb-9294-e4852a84e201]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment