Golang gin框架cookie生成问题

问题描述 投票:0回答:2

这是我的代码,我从 go-gin 框架文档中复制它,我的问题是当

createCookie
函数命中时它返回“NotSet”,我必须运行相同的 API 回调来获取 cookie。

// createCookie accepts c as gin context and create cookies
func createCookie(c *gin.Context) (string, error) {
    cookie, err := c.Cookie("device")

    if err != nil {
        cookie = "NotSet"
        c.SetCookie("device", "test", 3600, "/", os.Getenv("AUTH_DOMAIN"), true, true)
    }

    return cookie, nil
}
go
2个回答
1
投票
// createCookie accepts c as gin context and create cookies
func createCookie(c *gin.Context) (string, error) {
    cookie, err := c.Cookie("device")

    if err != nil {
        // Replae it with your initial value
        cookie = "NotSet"
        c.SetCookie("device", cookie, 3600, "/", os.Getenv("AUTH_DOMAIN"), true, true)
    }

    return cookie, nil
}

0
投票
    http.SetCookie(context.Writer, &http.Cookie{
    Name:     "token",
    Value:    url.QueryEscape(newToken),
    MaxAge:   maxAge,
    Path:     "/",
    Domain:   "",
    SameSite: http.SameSiteNoneMode,
    Secure:   true,
    HttpOnly: true,
})
© www.soinside.com 2019 - 2024. All rights reserved.