SetCookie()
sets the cookie on the ResponseWriter
's headers hence you can read its value in subsequent requests where it can be read using Request
object's Cookie()
method.
Here's the related code of the same to give you an idea:
func (c *Context) SetCookie(
name string,
value string,
maxAge int,
path string,
domain string,
secure bool,
httpOnly bool,
) {
if path == "" {
path = "/"
}
http.SetCookie(c.Writer, &http.Cookie{
Name: name,
Value: url.QueryEscape(value),
MaxAge: maxAge,
Path: path,
Domain: domain,
Secure: secure,
HttpOnly: httpOnly,
})
}
func (c *Context) Cookie(name string) (string, error) {
cookie, err := c.Request.Cookie(name)
if err != nil {
return "", err
}
val, _ := url.QueryUnescape(cookie.Value)
return val, nil
}
Update
You won't be able to access cookies in your page because you're passing HttpOnly
as true
. When this is set to true only the server has access to the cookies and you can't fetch their values in front-end using Javascript.