在Golang中设置Cookie(net / http)

我试图用Golang的networking/ 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) } 

我尝试用“cookies”search“Golang”,但没有得到任何好的结果。 如果有人能指出我正确的方向,将不胜感激。

谢谢。

我不是Go专家,但是我认为你是根据请求设置cookie的,不是。 您可能需要将其设置为响应。 net / http中有一个setCookie函数。 这可能有帮助: http : //golang.org/pkg/net/http/#SetCookie

 func SetCookie(w ResponseWriter, cookie *Cookie) 
 //ShowAllTasksFunc is used to handle the "/" URL which is the default ons func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request){ if r.Method == "GET" { context := db.GetTasks("pending") //true when you want non deleted notes if message != "" { context.Message = message } context.CSRFToken = "abcd" message = "" expiration := time.Now().Add(365 * 24 * time.Hour) cookie := http.Cookie{Name: "csrftoken",Value:"abcd",Expires:expiration} http.SetCookie(w, &cookie) homeTemplate.Execute(w, context) } else { message = "Method not allowed" http.Redirect(w, r, "/", http.StatusFound) } } 

RequestsResponseWriter之间有一个基本的区别,Request就是浏览器发送的内容

 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,因为cookie存储在域中,并且不能从跨域访问,如果将cookie设置为HTTP,则只能从网站设置它通过HTTP而不是通过JS。

所以从cookies获取信息时,可以使用r.Cookie方法来做到这一点

 cookie, _ := r.Cookie("csrftoken") if formToken == cookie.Value { 

https://github.com/thewhitetulip/Tasks/blob/master/views/addViews.go#L72-L75

但是当你要设置一个cookie的时候,你必须在响应编写器方法中完成这个请求,这个请求是一个我们回应的只读对象,把它看作是从某个人那里得到的文本消息,也就是一个请求,你只能得到它,你键入的是一个响应,所以你可以在input一个cookie

有关更多详细信息: https : //thewhitetulip.gitbooks.io/webapp-with-golang-anti-textbook/content/content/2.4workingwithform.html

这下面的代码可以帮助你

  cookie1 := &http.Cookie{Name: "sample", Value: "sample", HttpOnly: false} http.SetCookie(w, cookie1) 

以下显示我们如何在我们的产品中使用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) // ... } 

在我joinPath和MaxAge之前,Safari并不适合我。 安全和正规的cookies都为我工作

共享,这样可以帮助像我一样被困住2天以上的人:)

 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) 

你可以使用大猩猩包来处理cookies,或者我会说安全的cookies: http : //www.gorillatoolkit.org/pkg/securecookie