【问题标题】:Golang gin framework cookie generating issueGolang gin 框架 cookie 生成问题
【发布时间】:2021-10-19 00:12:03
【问题描述】:

这是我的代码,我从 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


    【解决方案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
    }
    

    【讨论】:

    • 谢谢,成功了:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 2012-12-12
    • 1970-01-01
    • 2016-11-03
    相关资源
    最近更新 更多