go-gin无法设置cookies

huangapple go评论121阅读模式
英文:

go-gin unable to set cookies

问题

我正在尝试在一个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。

英文:

I am trying to set a cookie on an HTML page

 func testCookie(c *gin.Context) {
    c.SetCookie("test1", "testvalue", 10, "/", "", true, true)
	c.HTML(200, "dashboard", gin.H{
		"title":    "Dashboard",
        }
    }

This should have set the cookie on the HTML page but it doesn't.
My server is running to serve https requests. I am not sure why I am not able to set cookies here.

答案1

得分: 4

在上面的评论中补充一点
尝试使用以下代码:

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

示例:

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

Adding to comments above
Try using

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

example

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

答案2

得分: 1

SetCookie() 方法将 cookie 设置到 ResponseWriter 的头部,因此您可以在后续的请求中读取其值,可以使用 Request 对象的 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
}

更新

由于您将 HttpOnly 设置为 true,因此无法在页面中访问 cookie。当设置为 true 时,只有服务器可以访问 cookie,您无法使用 JavaScript 在前端获取其值。

英文:

SetCookie() sets the cookie on the ResponseWriter's headers hence you can read its value in subsequent requests where it can be read using Request object's Cookie() method.

Here's the related code of the same to give you an idea:

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
}

Update

You won't be able to access cookies in your page because you're passing HttpOnly as true. When this is set to true only the server has access to the cookies and you can't fetch their values in front-end using Javascript.

huangapple
  • 本文由 发表于 2016年11月30日 19:44:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/40887538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定