【问题标题】:Pass the cookie file into the curl request nodejs将 cookie 文件传入 curl 请求 nodejs
【发布时间】: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


【解决方案1】:

npm cookiefile 包。它可以读取 netscape 格式的 cookie 文件并生成相应的标头。

它将从 cookie 文件中发送 cookie 及其所有过期、路径和范围数据。

类似这样的东西(未调试):

var http = require('http');
const cookiefile = require('cookiefile')

const cookiemap = new cookiefile.CookieMap('path/to/cookie.txt')
const cookies = cookiemap.toRequestHeader().replace ('Cookie: ','')

var options = {
    hostname: 'www.myaccount.google.com',
    path: '/',
    headers: {
        'User-Agent': 'whatever',
        'Referer': 'https://google.com/',
        'Cookies': cookies
    }
};

http.get(options, callback);

【讨论】:

  • 使用您的代码后出现错误“Cookie 文件不存在”。
  • 您是否在调用 CookieMap 构造函数时提供了 cookie 文件的实际路径?使用您的实际路径代替path/to/cookie.txt
【解决方案2】:

您似乎已经知道,您只需要根据与目标主机名对应的名称和值来设置Cookie 标头。这是developer.mozilla.org 域的 cookie 文件中的示例:

var http = require('http');

var options = {
    hostname: 'developer.mozilla.org',
    path: '/',
    headers: {
        'User-Agent': 'whatever',
        'Referer': 'https://google.com/',
        'Cookie': 'dwf_sg_task_completion=False; lux_uid=158048125271715522;'
    }
};

http.get(options, callback);

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 2021-12-27
    • 2017-10-17
    • 2014-08-13
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    相关资源
    最近更新 更多