codec
codec

Reputation: 8796

Setting before rendering html page go-gin

I am trying to set a cookie on an HTML page

 func testCookie(c *gin.Context) {
    c.SetCookie("test1", "testvalue", 10, "/", "", true, true)
    c.HTML(200, "dashboard", gin.H{
        "title":    "Dashboard",
        }
    }

This should have set the cookie on the HTML page but it doesn't. My server is running to serve https requests. I am not sure why I am not able to set cookies here. I am using google-chrome and ideally I should have been able to see the cookie there.

Upvotes: 1

Views: 785

Answers (1)

jmaloney
jmaloney

Reputation: 12280

The issue is with your maxAge input. Your current code instructs the browser to delete your cookie in 10 seconds.

Gin is wrapping http.SetCookie and creating a http.Cookie for you. To better understand what is happening you should read through those two links.

MaxAge=0 means no 'Max-Age' attribute specified.

MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'

MaxAge>0 means Max-Age attribute present and given in seconds

Upvotes: 2

Related Questions