İçeriğe geç

Çerez

Gin, yanıt ve istekte HTTP çerezlerini ayarlamak ve okumak için yardımcılar sağlar.

SetCookie parametreleri

c.SetCookie() metod imzası şöyledir:

c.SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
ParametreAçıklama
nameÇerez adı (anahtar).
valueÇerez değeri.
maxAgeSaniye cinsinden yaşam süresi. Çerezi silmek için -1, tarayıcı kapandığında silinen bir oturum çerezi yapmak için 0 ayarlayın.
pathÇerezin geçerli olduğu URL yolu. Site genelinde kullanılabilir kılmak için "/" kullanın.
domainÇerezin kapsamlandığı alan (ör., "example.com"). Geliştirme sırasında "localhost" kullanın.
securetrue olduğunda, çerez yalnızca HTTPS bağlantıları üzerinden gönderilir. Üretimde bunu true olarak ayarlayın.
httpOnlytrue olduğunda, çerez istemci tarafı JavaScript’e (document.cookie) erişilemez, bu XSS saldırılarını önlemeye yardımcı olur. Üretimde bunu true olarak ayarlayın.

Çerez ayarlama ve alma

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

Deneyin

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

Çerezi silme

Max age’i -1 olarak ayarlayarak bir çerezi silin.

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

http.Cookie ile çerez ayarlama (v1.11+)

Gin, Expires, MaxAge, SameSite ve Partitioned gibi alanlara erişim sağlayan bir *http.Cookie kullanarak çerez ayarlamayı da destekler.

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

Ayrıca bakınız