dongyong8071 2018-02-07 16:52
浏览 92
已采纳

什么是命名cookie?

I am reading official documentation about Go language and found:

Cookie returns the named cookie provided in the request

When I try req.Cookie("My-Cookie") I receive named cookie not present

While when I fmt.Println(req.Cookies()) I receive following string:

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

What is the named cookie then?

  • 写回答

1条回答 默认 最新

  • dourang20110122 2018-02-08 19:36
    关注

    Here is a golang playground. It shows that what the OP posted works, so the bug is somewhere else. It also answers the question by showing what the named cookie is.

    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)
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?