Setting cookies with net/http

前端 未结 7 1457
说谎
说谎 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:10

    Below shows how we use cookie in our product:

    func handleFoo(w http.ResponseWriter, r *http.Request) {
    
        // cookie will get expired after 1 year 
        expires := time.Now().AddDate(1, 0, 0)
    
        ck := http.Cookie{
            Name: "JSESSION_ID",
            Domain: "foo.com",
            Path: "/",
            Expires: expires,
        }
    
        // value of cookie    
        ck.Value = "value of this awesome cookie"
    
        // write the cookie to response
        http.SetCookie(w, &ck)
    
        // ...
    }
    

提交回复
热议问题