-
cookie提示http: named cookie not present
- 在設定cookie之後,去獲取同一本地的cookie內容時,提示named cookie not present錯誤
-
[code]
func setCookie(w http.ResponseWriter, r *http.Request) { expiration := time.Now().Add(time.Hour) http.SetCookie(w, &http.Cookie{ Name: "access_token", Value: "cookie get test", Expires: expiration, HttpOnly: true, }) redirectURL := "/" http.Redirect(w, r, redirectURL, 200) } func getCookie(w http.ResponseWriter, r *http.Request) { c, err := r.Cookie("access_token") if err != nil { fmt.Fprintf(w, "Cannot get the cookie %s", err) log.Fatal(err) } tokenString := c.Value fmt.Fprintln(w, tokenString) }
- 搜尋相關問題,stackoverflow有相關說明:https://stackoverflow.com/questions/329364...
- 主要是與secure相關,帶有secure:true的使用者訪問的cookie不會通過http傳送cookies
- 將security設定為false後,使用chrom呼叫仍然不行,但在postman和其他瀏覽器是ok的,貌似與chrom的setting相關
本作品採用《CC 協議》,轉載必須註明作者和本文連結