【发布时间】:2020-01-31 14:24:34
【问题描述】:
我使用 chrome 的 cookies.txt 扩展名在登录谷歌后获取 cookies.txt。 cookie 文件格式为 netscape cookie。现在我想把这个cookie文件传递给nodejs的http请求。
我使用的命令行:
curl -L -b cookie.txt https://myaccount.google.com/
但是我找不到任何文档告诉我如何将 cookie 文件传递给 nodejs 的 curl 函数。 上面的命令行如何转成nodejs?
更新: cookie.txt 格式如下:
# HTTP Cookie File for mozilla.org by Genuinous @genuinous.
# To download cookies for this tab click here, or download all cookies.
# Usage Examples:
# 1) wget -x --load-cookies cookies.txt "https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Functions/Arrow_functions"
# 2) curl --cookie cookies.txt "https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Functions/Arrow_functions"
# 3) aria2c --load-cookies cookies.txt "https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Functions/Arrow_functions"
#
.mozilla.org TRUE / FALSE 1643553591 _ga GA1.2.176633766.1564724252
.developer.mozilla.org TRUE / TRUE 1583073592 dwf_sg_task_completion False
.mozilla.org TRUE / FALSE 1580567991 _gid GA1.2.1169610322.1580463999
developer.mozilla.org FALSE / FALSE 1580483392 lux_uid 158048125271715522
.mozilla.org TRUE / FALSE 1580481652 _gat 1
Nodejs 代码:
var http = require('http');
var options = {
hostname: 'www.myaccount.google.com',
path: '/',
headers: {
'User-Agent': 'whatever',
'Referer': 'https://google.com/',
'Cookie': ????
}
};
http.get(options, callback);
【问题讨论】:
-
在 node.js 中,您需要使用您正在发出的请求设置 cookie header。如果您显示用于发出请求的 nodejs 代码并显示您的 cookie 文件,那么人们可以更具体地帮助您。
-
我更新了 cookie.txt 格式。
-
cookie 标头类似于:
Cookie: name=value; name2=value2; name3=value3,因此示例文件中的第一个 cookie 将是Cookie: _ga=GA1.2.176633766.1564724252;
标签: node.js cookies http-headers httprequest