net/http
http cookie的定义
先来看下golang对cookie结构体的定义:
type Cookie struct {
Name string
Value string
Path string // optional
Domain string // optional
Expires time.Time // optional
RawExpires string // for reading cookies only
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int
Secure bool
HttpOnly bool
Raw string
Unparsed []string // Raw text of unparsed attribute-value pairs
}
常用参数:
Name
Value
Domain
Expires
HttpOnly
SecureMaxAge
服务端设置cookie
了解的cookie的属性,我们可以在服务端对cookie进行设置。
COOKIE_MAX_MAX_AGE = time.Hour * 24 / time.Second // 单位:秒。
maxAge = int(COOKIE_MAX_MAX_AGE)
uid:="10"
uid_cookie:=&http.Cookie{
Name: "uid",
Value: uid,
Path: "/",
HttpOnly: false,
MaxAge: maxAge
}
http.SetCookie(c.Writer,uid_cookie)
浏览器记录cookie
服务端获取cookie
var c = *gin.Context
uid, err := c.Request.Cookie("uid")