Cookies
Added: v1.0.0
Zen doesn't wrap cookie operations - net/http provides everything you need directly. Cookies are accessed through the standard request/response objects.
Set a Cookie
Use the standard http.SetCookie on the response writer:
go
http.SetCookie(c.Response, &http.Cookie{
Name: "session",
Value: sessionID,
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
Get a Single Cookie
Read a cookie value from the request. Returns http.ErrNoCookie if not present:
go
cookie, err := c.Request.Cookie("session")
if err != nil {
// cookie not found
}
value := cookie.Value1
2
3
4
5
2
3
4
5
List All Cookies
Iterate over all cookies sent with the request:
go
for _, cookie := range c.Request.Cookies() {
log.Printf("%s=%s", cookie.Name, cookie.Value)
}1
2
3
2
3
Delete a Cookie
Set the cookie with MaxAge: -1 or an expired Expires date:
go
http.SetCookie(c.Response, &http.Cookie{
Name: "session",
Path: "/",
MaxAge: -1,
})1
2
3
4
5
2
3
4
5
Cross-Origin Protection Cookies
The CrossOriginProtection middleware does not use cookies - it relies solely on the Sec-Fetch-Site and Origin headers sent by browsers. See the Cross-Origin Protection docs for details.