什么是命名cookie?

时间:2018-02-07 16:52:21

标签: http go cookies

我正在阅读关于Go语言的official documentation并找到:

  

Cookie返回请求中提供的命名cookie

当我尝试req.Cookie("My-Cookie")时,我收到named cookie not present

当我fmt.Println(req.Cookies())时收到以下字符串:

[My-Cookie=a783e7e4-c331-4802-6544-7374f5128882 Path=/svc Expires="Tue, 07 Feb 2068 16:05:53 GMT" Path=/svc HttpOnly=]

那么命名的cookie是什么?

1 个答案:

答案 0 :(得分:2)

这是golang playground。它显示了OP发布的内容,因此bug在其他地方。它还通过显示命名cookie的内容来回答问题。

package main

import (
  "fmt"
  "net/http"
)

func main() {
  r := &http.Request{
    Header: http.Header{
      "Cookie": []string{
        "My-Cookie=a783e7e4-c331-4802-6544-7374f5128882 Path=/svc Expires=Tue, 07 Feb 2068 16:05:53 GMT Path=/svc HttpOnly=",
      },
    },
  }

  fmt.Println(r.Cookies())

  c, err := r.Cookie("My-Cookie")
  if err != nil {
    fmt.Println("Error:", err)
    return
  }

  // only cookie name and value are parsed
  fmt.Println("Name", c.Name)
  fmt.Println("Value", c.Value)

}
相关问题