Can't get cookie expiration time in golang

2019-04-28 03:20发布

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)

Then i'm trying to get it

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

Output

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

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

标签: http cookies go
2条回答
2楼-- · 2019-04-28 03:49

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.

查看更多
贼婆χ
3楼-- · 2019-04-28 04:05

If you want to query the expiry, you should address it as a different cookie.

查看更多
登录 后发表回答