Cookies go gin无法设置Cookie

Cookies go gin无法设置Cookie,cookies,go,go-gin,Cookies,Go,Go Gin,我正在尝试在HTML页面上设置cookie func testCookie(c *gin.Context) { c.SetCookie("test1", "testvalue", 10, "/", "", true, true) c.HTML(200, "dashboard", gin.H{ "title": "Dashboard", } } 这应该在HTML页面上设置cookie,但它没有。 我的服务器正在运行以服务https请

我正在尝试在HTML页面上设置cookie

 func testCookie(c *gin.Context) {
    c.SetCookie("test1", "testvalue", 10, "/", "", true, true)
    c.HTML(200, "dashboard", gin.H{
        "title":    "Dashboard",
        }
    }
这应该在HTML页面上设置cookie,但它没有。 我的服务器正在运行以服务https请求。我不知道为什么我不能在这里设置cookie。

ResponseWriter
的标题上设置cookie,因此您可以在后续请求中读取其值,可以使用对象的
cookie()
方法读取该值

下面是同样的例子,让你有一个想法:

func (c *Context) SetCookie(
    name string,
    value string,
    maxAge int,
    path string,
    domain string,
    secure bool,
    httpOnly bool,
) {
    if path == "" {
        path = "/"
    }
    http.SetCookie(c.Writer, &http.Cookie{
        Name:     name,
        Value:    url.QueryEscape(value),
        MaxAge:   maxAge,
        Path:     path,
        Domain:   domain,
        Secure:   secure,
        HttpOnly: httpOnly,
    })
}

func (c *Context) Cookie(name string) (string, error) {
    cookie, err := c.Request.Cookie(name)
    if err != nil {
        return "", err
    }
    val, _ := url.QueryUnescape(cookie.Value)
    return val, nil
}

更新 您将无法访问页面中的Cookie,因为您正在通过。当此设置为true时,只有服务器可以访问cookie,并且您不能使用Javascript在前端获取它们的值。

添加到上面的注释中 试用

c.SetCookie("cookieName", "name", 10, "/", "yourDomain", true, true)
范例

c.SetCookie("gin_cookie", "someName", 60*60*24, "/", "google.com", true, true)

Cookie是在响应中设置的,您正在查看请求中存在的Cookie。收到问题的答案后,请不要完全编辑您的问题。
c.SetCookie(“test1”,“testvalue”,10,“/”,“”,true,true)c.HTML(200,“dashboard”,gin.H{“title”:“dashboard”,}
理想情况下,这应该在HTML页面上设置cookie,但它不是working@love2code在HTML页面中设置cookie是什么意思?cookie是标题的一部分。如果您将HttpOnly作为True传递,则也无法使用Javascript访问cookie。@love2code在此处阅读有关HttpOnly cookie的更多信息: