Cookies 使用net/http设置Cookie

Cookies 使用net/http设置Cookie,cookies,go,Cookies,Go,我正在尝试使用Go的net/http包设置Cookie。我有: package main import "io" import "net/http" import "time" func indexHandler(w http.ResponseWriter, req *http.Request) { expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{"test", "tcookie", "/", "www.

我正在尝试使用Go的net/http包设置Cookie。我有:

package main

import "io"
import "net/http"
import "time"

func indexHandler(w http.ResponseWriter, req *http.Request) {
    expire := time.Now().AddDate(0, 0, 1)
    cookie := http.Cookie{"test", "tcookie", "/", "www.domain.com", expire, expire.Format(time.UnixDate), 86400, true, true, "test=tcookie", []string{"test=tcookie"}}
    req.AddCookie(&cookie)
    io.WriteString(w, "Hello world!")
}

func main() {
    http.HandleFunc("/", indexHandler)
    http.ListenAndServe(":80", nil)
}

我试着用“饼干”在谷歌上搜索“咕噜”,但没有得到任何好结果。如果有人能为我指出正确的方向,我将不胜感激。

我不是围棋专家,但我认为你是在根据请求设置cookie,不是吗?您可能希望在响应上设置它。net/http中有一个
setCookie
函数。这可能有助于:


下面的代码帮助您

    cookie1 := &http.Cookie{Name: "sample", Value: "sample", HttpOnly: false}
    http.SetCookie(w, cookie1)
请求
响应编写器
之间有一个基本区别,请求就是浏览器发送的请求

Host: 127.0.0.1:8081
User-Agent: ...
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Referer: http://127.0.0.1:8081/
Cookie: csrftoken=abcd
Connection: keep-alive
响应是处理程序将发送的,类似于:

Content-Type: text/html; charset=utf-8
Date: Tue, 12 Jan 2016 16:43:53 GMT
Set-Cookie: csrftoken=abcd; Expires=Wed, 11 Jan 2017 16:43:53 GMT
Transfer-Encoding: chunked
<html>...</html>

但是当你要设置一个cookie时,你必须在response writer方法中进行设置,请求是一个只读对象,我们对它进行响应,把它想象成你从某人那里收到的一条文本消息,这是一个请求,你只能得到它,你键入的是一个响应,所以你可以随时键入cookie


有关更多详细信息:

您可以使用gorilla软件包来处理cookie,或者我会说安全cookie:

下面显示了我们如何在产品中使用cookie:

func handleFoo(w http.ResponseWriter, r *http.Request) {

    // cookie will get expired after 1 year 
    expires := time.Now().AddDate(1, 0, 0)

    ck := http.Cookie{
        Name: "JSESSION_ID",
        Domain: "foo.com",
        Path: "/",
        Expires: expires,
    }

    // value of cookie    
    ck.Value = "value of this awesome cookie"

    // write the cookie to response
    http.SetCookie(w, &ck)

    // ...
}

在Safari中,在我添加Path和MaxAge之前,它对我不起作用。安全饼干和普通饼干对我都有效

分享,以帮助像我一样被困超过2天的人:)


首先,您需要创建Cookie,然后使用http包的SetCookie()函数设置Cookie

expire := time.Now().Add(10 * time.Minute) 
cookie := http.Cookie{Name: "User", Value: "John", Path: "/", Expires: expire, MaxAge: 90000}
http.SetCookie(w, &cookie)

谢谢这似乎奏效了。我刚才看错了是的,很混乱。如果你的go程序充当HTTP客户端,并且你想向HTTP服务器发送cookie值,那么你需要Request.AddCookie…你能演示如何创建cookie并设置它吗?谢谢,这对我来说也很有效,只不过放置路径不够谢谢deepakssn!你是救世主!在我的例子中,cookie集在没有设置路径的情况下无法工作:
http.cookie{Name:“csrftoken”,值:“abcd”,过期:过期,路径:“/”}
cookie, _ := r.Cookie("csrftoken")
if formToken == cookie.Value {
func handleFoo(w http.ResponseWriter, r *http.Request) {

    // cookie will get expired after 1 year 
    expires := time.Now().AddDate(1, 0, 0)

    ck := http.Cookie{
        Name: "JSESSION_ID",
        Domain: "foo.com",
        Path: "/",
        Expires: expires,
    }

    // value of cookie    
    ck.Value = "value of this awesome cookie"

    // write the cookie to response
    http.SetCookie(w, &ck)

    // ...
}
expire := time.Now().Add(20 * time.Minute) // Expires in 20 minutes
cookie := http.Cookie{Name: "username", Value: "nonsecureuser", Path: "/", Expires: expire, MaxAge: 86400}
http.SetCookie(w, &cookie)
cookie = http.Cookie{Name: "secureusername", Value: "secureuser", Path: "/", Expires: expire, MaxAge: 86400, HttpOnly: true, Secure: true}
http.SetCookie(w, &cookie)
expire := time.Now().Add(10 * time.Minute) 
cookie := http.Cookie{Name: "User", Value: "John", Path: "/", Expires: expire, MaxAge: 90000}
http.SetCookie(w, &cookie)