Skip to content

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.

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,
})

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.Value

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)
}

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

go
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.

Released under the MIT License.