Good morning everyone,
I'm trying to build a test web-app with Go, but I can't seem to find a way to properly set the cookies. I'm using the securecookie function from gorilla package, and it works when I try it on the same host (while opening a browser to localhost:port I can correctly find the cookie set).
However, if I try with another computer to connect to the host (192.168.x.x.:port) the code works perfectly but the cookie is not set, I cannot find it inspecting the page.
Here's the code:
package main
import (
"net/http"
"github.com/gorilla/securecookie"
)
// Secure cookie instance used later
var hashKey = []byte(securecookie.GenerateRandomKey(64))
var blockKey = []byte(securecookie.GenerateRandomKey(32))
var s = securecookie.New(hashKey, blockKey)
// Generates a secure cookie, returning the cookie
func GenerateSecureCookie(name, value string) *http.Cookie {
data := map[string]string{name: value}
cookie := &http.Cookie{}
if encoded, err := s.Encode(name, data); err == nil {
cookie.Name = name
cookie.Value = encoded
cookie.Secure = true
cookie.HttpOnly = true
} else {
panic(err)
}
return cookie
}
func handlerFunction(w http.ResponseWriter, r *http.Request) {
cookie := GenerateSecureCookie("testcookie", "a random value")
// Set the cookie
http.SetCookie(w, cookie)
w.Write([]byte("Hello world"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", handlerFunction)
http.ListenAndServe(":3334", mux)
}
Any hint about why this happens? Thank you!