Cookies on localhost with explicit domain

前端 未结 21 2370
[愿得一人]
[愿得一人] 2020-11-22 04:17

I must be missing some basic thing about cookies. On localhost, when I set a cookie on server side and specify the domain explicitly as localhost (or .localhost). t

21条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 05:07

    After much experimentation and reading various posts, this worked. I could set multiple cookies, read them back and set the time negative and delete them.

    func addCookie(w http.ResponseWriter, name string, value string) {
        expire := time.Now().AddDate(0, 0, 1)
        cookie := http.Cookie{
           Name:    name,
           Value:   value,
           Expires: expire,
           Domain:  ".localhost",
           Path:    "/",
        }
        http.SetCookie(w, &cookie)
    }
    

提交回复
热议问题