首页 > 解决方案 > 无法在浏览器中设置 cookie,但使用邮递员

问题描述

我正在尝试在 chrome 浏览器中使用 go 设置一个 cookie,但只能在 postman 中设置它。

cookie 在邮递员中成功设置,但根本没有在 chrome 中设置,没有错误。我已经使用这个包https://github.com/gin-contrib/cors将 CORS 设置为默认值,因为我过去遇到过 CORS 请求的问题,这似乎解决了这个问题。我使用以下方式设置cookie:

c.SetCookie(
    "TOKEN",
    tokenString,
    3600,
    "/",
    "localhost",
    false,
    true)

我尝试用http://127.0.0.1替换 localhost ,它再次适用于邮递员,但不适用于 chrome 或 firefox。我也尝试过使用 http 包设置 cookie,但这同样失败了。

编辑我也尝试将域设置为空白(“”),但这具有相同的结果。

标签: gocookiesbrowser

解决方案


您传递trueSetCookiesecure的参数,但您将 cookie 发送到不安全的 http 地址。

根据MDN 文档

A secure cookie is only sent to the server with an encrypted request over the HTTPS protocol.

推荐阅读