简体   繁体   English

使用 NodeJS 设置请求标头的 HTTPS

[英]HTTPS with NodeJS set request headers

Hey guys I'm struggling a little bit with Node JS and HTTPS requests.嘿伙计们,我在 Node JS 和 HTTPS 请求方面有点挣扎。 The following code actually works fine but I don't know how to set custom request headers like X-Forwarded-For and User Agent.以下代码实际上工作正常,但我不知道如何设置自定义请求标头,如 X-Forwarded-For 和用户代理。 Is there a way to add these?有没有办法添加这些?

Thanks :)谢谢 :)

Here is my code:这是我的代码:

    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();

options argument accepts a headers property. options参数接受headers属性。

const options = {
  hostname: 'offertoro.com',
  port: 443,
  path: '/',
  method: 'GET',
  headers: {
     'X-Forwarded-For': 'xxx',
     'User-Agent': 'Foo'
  }
};

我认为您在谈论response标头而不是请求标头,因为请求标头是在请求正文中发送给您的,您收到它们不是您设置它们而是您设置response标头,当您响应任何如果是这种情况,您可以通过设置响应头

res.header('headersName','headersVALUE')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM