Node.js 带有NodeJS的HTTPS设置请求头
嘿,伙计们,我在节点JS和HTTPS请求方面有点挣扎。下面的代码实际上工作得很好,但我不知道如何设置自定义请求头,如X-Forwarded-For和User-Agent。有没有办法添加这些 谢谢:) 这是我的密码:Node.js 带有NodeJS的HTTPS设置请求头,node.js,https,Node.js,Https,嘿,伙计们,我在节点JS和HTTPS请求方面有点挣扎。下面的代码实际上工作得很好,但我不知道如何设置自定义请求头,如X-Forwarded-For和User-Agent。有没有办法添加这些 谢谢:) 这是我的密码: const https = require('https'); const options = { hostname: 'offertoro.com', port: 443, path: '/', method: 'GET' }; const req =
const https = require('https');
const options = {
hostname: 'offertoro.com',
port: 443,
path: '/',
method: 'GET'
};
const req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.end();
我想你说的是
response
头而不是请求头,因为请求头是在请求体中发送给你的,你接收它们而不是设置它们
而是设置response
标题,当您响应任何操作时,将发送这些标题
如果是这种情况,您可以通过
res.header('headersName','headersVALUE')
参数接受标题
属性
const options = {
hostname: 'offertoro.com',
port: 443,
path: '/',
method: 'GET',
headers: {
'X-Forwarded-For': 'xxx',
'User-Agent': 'Foo'
}
};
是的,这就是解决办法。谢谢你,伙计