Redirect with cookie

5,483 views
Skip to first unread message

Paulo Coutinho

unread,
Jul 8, 2015, 10:22:52 AM7/8/15
to golan...@googlegroups.com
Hi,

Im trying save cookie and make a redirect with move perm (302) function, but when i redirect the cookies are not saved.

Can anyone help me about how i can do it correctly?

The solution today is show a success response in the action that i create the cookie, but i want create the cookie and redirect to admin home.

Enviado do meu iPhone

Kiki Sugiaman

unread,
Jul 8, 2015, 12:42:38 PM7/8/15
to golan...@googlegroups.com
Move perm is 301, 302 is found. Assuming you already use http.Status_____ const, this is already taken care of. Otherwise try fixing the status.

The redirection behavior is also browser dependent. So you may want to try different ones. Setting the cookie is all a go app can do. How it's interpreted by the browser is entirely something else.

Paulo Coutinho

unread,
Jul 8, 2015, 4:56:31 PM7/8/15
to golan...@googlegroups.com
Yes, i change when write. But the problem persists with one or other.

If i render without redirect, no problems. But create cookie and redirect is the problem.

Kiki Sugiaman

unread,
Jul 8, 2015, 5:10:46 PM7/8/15
to golan...@googlegroups.com
Did you redirect to the same domain?

I do a lot of http.Redirect(w, r, referer, http.StatusFound) followed by http.SetCookie(w, cookie) without much problem.

I suspect the problem is somewhere else. Without more info, it will be one guess after another. Try to create a reproducible case.

Paulo Coutinho

unread,
Jul 9, 2015, 7:38:52 PM7/9/15
to golan...@googlegroups.com
Hi,

My code is:

func panelLoginProcess(c *gin.Context) {
    token := getSHA1Hash("2132323213")
    expiration := time.Now().Add(365 * 24 * time.Hour)
    cookie := http.Cookie{Name: "XYZ-SESSION", Value: token, Expires: expiration}
    http.SetCookie(c.Writer, &cookie)
    c.Redirect(http.StatusMovedPermanently, "/panel/home/index")
}


What is wrong? Thanks.




Volker Dobler

unread,
Jul 17, 2015, 4:27:03 AM7/17/15
to golan...@googlegroups.com

Am Freitag, 10. Juli 2015 01:38:52 UTC+2 schrieb Paulo Coutinho:
Hi,

My code is:

func panelLoginProcess(c *gin.Context) {
    token := getSHA1Hash("2132323213")
    expiration := time.Now().Add(365 * 24 * time.Hour)
    cookie := http.Cookie{Name: "XYZ-SESSION", Value: token, Expires: expiration}
    http.SetCookie(c.Writer, &cookie)
    c.Redirect(http.StatusMovedPermanently, "/panel/home/index")
}


What is wrong? Thanks.


The path attribute of the cookie depends on the path your panelLoginProcess
was invoked which might be /foo/bar/waz.html resulting in a path attribute
of /foo/bar. So this cookie will be sent only to ULRs starting with /foo/bar/
and not your /panel/home/index.
Try setting the Path explicitly to "/".

V.  
Reply all
Reply to author
Forward
0 new messages