我试图在 chrome 浏览器中使用 go 设置 cookie,但只能在 postman 中设置它。
cookie 在 postman 中成功设置,但在 chrome 中没有正确设置。 我已使用此包将 CORS 设置为默认值 https://github.com/gin-contrib/cors因为我过去遇到过 CORS 请求的问题,这似乎解决了问题。 我使用以下方法设置 cookie:
c.SetCookie(
"TOKEN",
tokenString,
3600,
"/",
"localhost",
false,
true)
我尝试用 http://127.0.0.1 替换本地主机这再次适用于 postman ,但不适用于 chrome 或 firefox。我也尝试过使用 http 包设置 cookie,但这以同样的方式失败了。
编辑 我也尝试将域设为空白 (""),但结果相同。
最佳答案
您将 true
传递给了 SetCookie 的 secure
参数但您将 cookie 发送到不安全的 http 地址。
根据 the MDN docs :
A secure cookie is only sent to the server with an encrypted request over the HTTPS protocol.
关于go - 无法在浏览器中设置 cookie 但与 postman 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57748407/