无法在golang中获取cookie到期时间 [英] Can't get cookie expiration time in golang

查看:828
本文介绍了无法在golang中获取cookie到期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置新的Cookie

I'm setting a new cookie

func f1(w http.ResponseWriter, r *http.Request) {
...
expire := time.Now().AddDate(0, 1, 0)
cookie := http.Cookie{"token", token, "/", "domain", expire, expire.Format(time.UnixDate), 86400, true, true, "token=" + token, []string{"token=" + token}}
http.SetCookie(w, &cookie)

然后我试图获取它

func f2(w http.ResponseWriter, r *http.Request) {
...
cookie, err := r.Cookie("token")
fmt.Println(cookie.Value)
fmt.Println(cookie.Expires)

输出

valid_token_string
0001-01-01 00:00:00 +0000 UTC

值是相同的i设置,但Expires为空。
为什么?

Value is the same i set, but Expires is empty. Why?

推荐答案

expires属性只与Set-Cookie响应头一起发送,而不与Cookie请求头一起发送。 Cookie请求标头只包含Cookie的名称和值,而不包含任何其他元数据。

That's how HTTP works; the expires attribute is only sent with the Set-Cookie response header, not with the Cookie request header. The Cookie request header contains only the names and values of the cookies, not any other metadata.

这篇关于无法在golang中获取cookie到期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆