I am having issues setting sessions on one request and getting them on another page request.
I am using the mysqlstore package which was recommended on the gorilla sessions github page under the "Store Implementations" section in there readme file.
https://github.com/srinathgs/mysqlstore
I setup a simple request to test it out and it wasn't working. Here is the code:
HomeController
func (this *HomeController) SetCookie(ctx types.AppContext) web.HandlerType {
return func(c web.C, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Printf("%v
", r)
ctx.SetCookies(r, w)
}
}
func (this *HomeController) GetCookie(ctx types.AppContext) web.HandlerType {
return func(c web.C, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Printf("%v
", r)
ctx.GetCookies(r)
}
}
types.AppContext
func (this *AppContext) SetCookies(r *http.Request, w http.ResponseWriter) {
fmt.Println("Setting cookies")
session, _ := this.store.Get(r, "session-name")
// Set some session values.
session.Values["foo"] = "bar"
session.Values[42] = 43
// Save it before we write to the response/return from the handler.
err := session.Save(r, w)
if err != nil {
fmt.Println("Problem Saving session data")
}
}
func (this *AppContext) GetCookies(r *http.Request) {
session, _ := this.store.Get(r, "session-name")
fmt.Println("Getting cookies")
// get some session values
for k, v := range session.Values {
fmt.Println("Key:", k)
fmt.Println("Value:", v)
}
}
main.go
ctx.SetStore("sessions", "/", 3600, []byte("somesecret"))
....
goji.Get("/set-cookie", homeCtrl.SetCookie(ctx))
goji.Get("/get-cookie", homeCtrl.GetCookie(ctx))
When I visit both pages I get no errors and I can see in the database a session was saved. However, when I try to retrieve the saved cookie, the session.Values map is empty.
When I print out the request I get the following (formatted for easy viewing):
&{
GET /get-cookie HTTP/1.1 1 1
map[
Connection:[keep-alive]
User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.107 Safari/537.36]
Cookie:[session-name=MTQ0MDQ4MjU0M3xCUXdBQWpJM3x3xeRstTXbooJerGOhzP2pvEmqIkisE1XjS76zI365pA==]
Accept-Language:[en,en-CA;q=0.8,en-US;q=0.6,ja;q=0.4]
Cache-Control:[max-age=0]
Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8]
Upgrade-Insecure-Requests:[1]
Accept-Encoding:[gzip, deflate, sdch]
]
0x66a9a0 0 []
false localhost:8000 map[]
map[]
<nil>
map[]
[::1]:62375
/get-cookie
<nil>
}
As you can see I am sending a cookie and it is the "session-name" one. In Chrome when I look at the cookies I can see it locally as well.
Name: session-name
Value: MTQ0MDQ4MjU0M3xCUXdBQWpJM3x3xeRstTXbooJerGOhzP2pvEmqIkisE1XjS76zI365pA
Domain: localhost
Path: /
Expires/Max-Age: 2015-08-25T07:02:23.347Z (it was about 10:50pm - 11:00pm when I tested this out)
I have been looking at this and can't figure out why it doesn't work. This is as simple as I can make it and according to the examples this should work but it doesn't. Is there something I am missing in my code to make it work?