gpt4 book ai didi

node.js - 将cookie文件传入curl请求nodejs

转载 作者:太空宇宙 更新时间:2023-11-03 23:12:02 27 4
gpt4 key购买 nike

我在登录google后使用chrome的cookies.txt扩展来获取cookies.txt。cookie 文件格式是netscape cookie。现在我想通过nodejs将此cookie文件传递给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);

最佳答案

这是npm cookiefile包裹。它可以读取netscape格式的cookie文件并生成适当的 header 。

它将发送 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);

关于node.js - 将cookie文件传入curl请求nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60005760/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com