HTTP Set Cookie Golang
func main() {
router := http.NewServeMux()
router.HandleFunc("/", (func(w http.ResponseWriter, r *http.Request) {
// set content type to json
w.Header().Set("Content-Type", "application/json")
// set cookie for storing token
cookie := http.Cookie{}
cookie.Name = "accessToken"
cookie.Value = "ro8BS6Hiivgzy8Xuu09JDjlNLnSLldY5"
cookie.MaxAge = time.Now().Minute() * 1
cookie.Secure = true
cookie.HttpOnly = true
cookie.SameSite = http.SameSiteStrictMode
http.SetCookie(w, &cookie)
json.NewEncoder(w).Encode(&ApiResponse{Code: http.StatusOK, Message: "Hello Wordl Using Nodemon"})
}))
err := http.ListenAndServe(":3000", router)
if err != nil {
log.Fatal(err)
}
}
Restu Wahyu Saputra