【发布时间】:2017-09-02 08:25:55
【问题描述】:
我希望我的 Go 应用程序通过网站进行身份验证,然后使用收到的 cookie 访问安全位置。以下 curl 示例准确地说明了我正在尝试做的事情:
通过x-www-form-urlencoded 对网站进行身份验证并保存 cookie。数据自动进行urlencoded:
curl 'https://www.example.com/login' \
--cookie-jar cookies.txt \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data 'user=USER NAME&pass=PASS'
现在身份验证 cookie 保存在 cookies.txt 中,我只是发送它以访问需要登录的页面:
curl 'https://www.example.com/profile' \
--cookie cookies.txt
我不想将 cookie 存储在我的应用程序的磁盘上,只存储在内存中,以便我可以在需要时使用它。
有没有人举例说明如何在 Go 中实现这一点?
【问题讨论】:
-
谢谢。很好和清晰的解释。我从我的身份验证函数返回一个指向
cookiejar.Jar的指针,并在另一个函数中将它与http.Client一起使用。
标签: cookies web go http-authentication