I would like my golang server to send back a cookie in HTTP raiponce that will be placed on the user's computer. Here is my code
func userLogin(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{
Name: "my-cookie",
Value: "some value",
}
http.SetCookie(w, cookie)
w.WriteHeader(200)
w.Write([]byte("cookie is set))
return
}
it is marked in the header
but I see no cookie to create
The browser defaults the cookie path to the request path.
It looks like you are setting the cookie in a request to /<something here>
, but are expecting the cookie in a request to /
.
Fix by setting the cookie path to /
.
func userLogin(w http.ResponseWriter, r *http.Request) {
cookie := &http.Cookie{
Name: "my-cookie",
Value: "some value",
Path: "/",
}
http.SetCookie(w, cookie)
w.WriteHeader(200)
w.Write([]byte("cookie is set))
}