تخطَّ إلى المحتوى

ملفات تعريف الارتباط

Gin provides helpers to set and read HTTP cookies on the response and request.

SetCookie parameters

The c.SetCookie() method signature is:

c.SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
ParameterDescription
nameThe cookie name (key).
valueThe cookie value.
maxAgeTime-to-live in seconds. Set to -1 to delete the cookie, or 0 to make it a session cookie (deleted when the browser closes).
pathThe URL path the cookie is valid for. Use "/" to make it available site-wide.
domainThe domain the cookie is scoped to (e.g., "example.com"). Use "localhost" during development.
secureWhen true, the cookie is only sent over HTTPS connections. Set this to true in production.
httpOnlyWhen true, the cookie is inaccessible to client-side JavaScript (document.cookie), which helps prevent XSS attacks. Set this to true in production.
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
router.GET("/cookie", func(c *gin.Context) {
cookie, err := c.Cookie("gin_cookie")
if err != nil {
cookie = "NotSet"
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
}
fmt.Printf("Cookie value: %s \n", cookie)
})
router.Run()
}

Try it

Terminal window
# First request -- no cookie sent, server sets one
curl -v http://localhost:8080/cookie
# Look for "Set-Cookie: gin_cookie=test" in the response headers
# Second request -- send the cookie back
curl -v --cookie "gin_cookie=test" http://localhost:8080/cookie
# Server logs: Cookie value: test

Delete a cookie by setting max age to -1.

c.SetCookie("gin_cookie", "test", -1, "/", "localhost", false, true)

Gin also supports setting cookies using an *http.Cookie, giving access to fields like Expires, MaxAge, SameSite, and Partitioned.

import (
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/set-cookie", func(c *gin.Context) {
c.SetCookieData(&http.Cookie{
Name: "session_id",
Value: "abc123",
Path: "/",
Domain: "localhost",
Expires: time.Now().Add(24 * time.Hour),
MaxAge: 86400,
Secure: true,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
// Partitioned: true, // Go 1.22+
})
c.String(http.StatusOK, "ok")
})
r.Run(":8080")
}

See also