ashishbaghudana
ashishbaghudana

Reputation: 429

Node.JS requests with rejectUnauthorized as false

I'm trying to use the request module for Node.js to formulate a HTTPS GET request. The corresponding code using the https module is as follows:

var https = require('https');

var options = {
    hostname: url,
    path: path,
    rejectUnauthorized: false,
    secureProtocol: 'TLSv1_method',
    port: 8443,
    method: 'POST'
  };

https.get(options, function(response) {
    var body = ''; 
    response.on('data', function(chunk) {
        body += chunk.toString();
    });
    response.on('end', function() {
        var content = JSON.parse(body);
        console.log(content);
    });
});

I attempted to rewrite this code using the request module as follows:

var request = require('request');

var options = {
    url: url,
    strictSSL: false
}

request.get(options, function(error, response, body) {
    if (error) {
        console.log(error);
    } else {
        console.log(body);
    }
});

However, this gives me an error { [Error: socket hang up] code: 'ECONNRESET', sslError: undefined }

What would be the request equivalent of rejectUnauthorized: false?

Upvotes: 12

Views: 36729

Answers (4)

M. Hamza Rajput
M. Hamza Rajput

Reputation: 10246

This will might help you.

let request = require( 'request' ).defaults({rejectUnauthorized:false});

Upvotes: 7

HHH
HHH

Reputation: 169

You can add an agent to your request call. This allows you to add rejectUnauthorized like before:

const request = require('request');
const https = require('https');

var agent = new https.Agent({
  host: URL_HOST,
  port: '443',
  path: '/',
  rejectUnauthorized: false
});

var options = {
  url: url,
  agent: agent
};

request.get(options, function(error, response, body) {
  if (error) {
    console.log(error);
  } else {
    console.log(body);
  }
});

Upvotes: 1

Mark Ryan Orosa
Mark Ryan Orosa

Reputation: 867

add these two line:

...
rejectUnauthorized: false,
requestCert: false,//add when working with https sites
agent: false,//add when working with https sites
...

that works for me.

Upvotes: 0

ashishbaghudana
ashishbaghudana

Reputation: 429

The request module in Node.js has the option secureProtocol even though it is not documented.

This can be used as follows:

var request = require('request');

var options = {
    url: url,
    strictSSL: false,
    secureProtocol: 'TLSv1_method'
}

request.get(options, function(error, response, body) {
    if (error) {
        console.log(error);
    } else {
        console.log(body);
    }
});

Upvotes: 6

Related Questions