Setting Cookies in Golang (net / http)

I am trying to set cookies using Golang net / http. I have

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) } 

I tried googling 'Golang' with 'cookies' but did not get good results. If someone can point me in the right direction, we will be very grateful.

Thank.

+49
cookies go
Aug 26 2018-12-12T00:
source share
6 answers

I'm not a Go expert, but I think you set cookies on demand, right. You might want to install it in response. There is a setCookie function in net / http. This may help: http://golang.org/pkg/net/http/#SetCookie

 func SetCookie(w ResponseWriter, cookie *Cookie) 
+66
Aug 26 2018-12-12T00:
source
 //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) } } 

There is a major difference between Requests and ResponseWriter , the request is what the browser will send, for example

 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 

and the response will be sent by the handler, for example:

 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> 

When the browser completes the request, it will contain a cookie for this domain, since cookies are stored in the domain and cannot be accessed from cross domains, if you set the cookie as HTTP only then it can only be accessed from the website that sets it through HTTP, not through JS.

Therefore, when you receive information from cookies, you can do this from the r.Cookie method, for example,

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

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

But when you are going to set a cookie, you have to do this in the response recording method, the request is a read-only object that we are responding to, think of it as a text message that you receive from someone, a request you can receive its only, what you enter is the answer, so you can enter a cookie in

for more details: https://thewhitetulip.gitbooks.io/webapp-with-golang-anti-textbook/content/content/2.4workingwithform.html

+8
Feb 08 '16 at 16:45
source

This code below helps u

  cookie1 := &http.Cookie{Name: "sample", Value: "sample", HttpOnly: false} http.SetCookie(w, cookie1) 
+4
Feb 08 '16 at 10:18
source

The following shows how we use cookies in our product:

 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) // ... } 
+2
Oct 12 '17 at 4:35 on
source

This did not work for me in Safari until I added Path and MaxAge. Both secure and regular cookies worked for me.

Divide so that he helps someone who is stuck like me for more than two days :)

 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) 
0
Oct 27 '17 at 5:27
source

You can use the gorilla package to process cookies or I would call secure cookies: http://www.gorillatoolkit.org/pkg/securecookie

-one
Sep 26 '16 at 18:46
source



All Articles