Setting cookies with net/http

前端 未结 7 1429
说谎
说谎 2020-12-12 23:12

I\'m trying to set cookies with Go\'s net/http package. I have:

package main

import \"io\"
import \"net/http\"
import \"time\"

func indexHandler(w http.Res         


        
相关标签:
7条回答
  • 2020-12-13 00:11

    It was not working for me in Safari until I added the Path and MaxAge. Both secure and regular cookies worked for me

    Sharing so that it helps someone who is stuck like me for more than 2 days :)

    expire := time.Now().Add(20 * time.Minute) // Expires in 20 minutes
    cookie := http.Cookie{Name: "username", Value: "nonsecureuser", Path: "/", Expires: expire, MaxAge: 86400}
    http.SetCookie(w, &cookie)
    cookie = http.Cookie{Name: "secureusername", Value: "secureuser", Path: "/", Expires: expire, MaxAge: 86400, HttpOnly: true, Secure: true}
    http.SetCookie(w, &cookie)
    
    0 讨论(0)
提交回复
热议问题