Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTPS with NodeJS set request headers

Tags:

node.js

https

Hey guys I'm struggling a little bit with Node JS and HTTPS requests. The following code actually works fine but I don't know how to set custom request headers like X-Forwarded-For and User Agent. 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();
like image 410
OberDulli94 Avatar asked Jun 17 '26 03:06

OberDulli94


1 Answers

options argument accepts a headers property.

const options = {
  hostname: 'offertoro.com',
  port: 443,
  path: '/',
  method: 'GET',
  headers: {
     'X-Forwarded-For': 'xxx',
     'User-Agent': 'Foo'
  }
};
like image 147
Marcos Casagrande Avatar answered Jun 19 '26 01:06

Marcos Casagrande