go - 无法在浏览器中设置 cookie 但与 postman 一起工作

标签 go cookies browser

我试图在 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 传递给了 SetCookiesecure 参数但您将 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/

相关文章:

Java/Javascript 将包含小程序的浏览器窗口置于前台

performance - 如何处理页面上数十万个 DOM 元素?

go - 在 Go 中查找地址的 BTC 余额

json - Golang 中的语法错误

go - 使用 channel 在 Go 中处理事件

spring - 如何在独立 Tomcat 中运行的 Spring Boot 应用程序中设置 cookie 名称?

javascript - 延迟 javascript 函数并仅显示一次

php - 从浏览器中删除 cookie?

java - 每当我在 Eclipse 中将浏览器设置为 DEFAULT WEB BROWSER(Chrome) 时,它都会显示错误。我该如何纠正它?

mongodb-go-driver/bson struct 到 bson.Document 编码