Skip to main content

Cookies

Zen doesn't wrap cookie operations - net/http provides everything you need directly. Cookies are accessed through the standard request/response objects.

Use the standard http.SetCookie on the response writer:

http.SetCookie(c.Response, &http.Cookie{
Name: "session",
Value: sessionID,
Path: "/",
HttpOnly: true,
Secure: true,
SameSite: http.SameSiteLaxMode,
})

Read a cookie value from the request. Returns http.ErrNoCookie if not present:

cookie, err := c.Request.Cookie("session")
if err != nil {
// cookie not found
}
value := cookie.Value

List All Cookies

Iterate over all cookies sent with the request:

for _, cookie := range c.Request.Cookies() {
log.Printf("%s=%s", cookie.Name, cookie.Value)
}

Set the cookie with MaxAge: -1 or an expired Expires date:

http.SetCookie(c.Response, &http.Cookie{
Name: "session",
Path: "/",
MaxAge: -1,
})

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.