chanhle
chanhle

Reputation: 168

How turn off curl's verification of the certificate in nodejs?

If I send curl -k "https://abc.com?querystring"

-k option to turn off curl's verification of the certificate

How to do this in nodejs If I want to make a GET request? How to override all http GET request do it in the same way?

Thank you for your support.

Upvotes: 0

Views: 3449

Answers (2)

josh3736
josh3736

Reputation: 144912

Set the rejectUnauthorized option to false.

var https = require('https');

var req = https.request({
    hostname: 'example.com',
    port: 443,
    path: '/',
    method: 'GET',
    rejectUnauthorized: false
}, function() { ... });

Upvotes: 1

Amol M Kulkarni
Amol M Kulkarni

Reputation: 21629

Check the following code:

var http = require('http');

var target = {    
    host : 'localhost',
    port : 3000,
    path : 'URL'
    //pfx: Certificate, Private key and CA certificates to use for SSL. Default is null.
    //cert: Public x509 certificate to use. Default null.
};

var Req_options = { 
    host: target.host,
    port: target.port,
    path: target.path,
    agent: false,
    method: 'GET'
};

callback = function(response) {
 var str = ''
  response.on('data', function (chunk) {
    str += chunk;
  });

  response.on('end', function () {
    console.log(str);
  });
}

var req = http.request(Req_options, callback);
req.end();


Updated as per comments:
In the above code, I have changed the https & target only as follows:

var https = require('https'); 

var target = {    
    host : 'www.google.co.in',
    port : 443,
    path : '/'
};

The output is as follows:

 </html>
    .........
    .........
    .........
    attachEvent&&window.attachEvent("onload",n);google.timers.load.t.prt=e=(new Date).getTime();})();
</script></body></html>

For more information, check this Node.js API docs

Upvotes: 0

Related Questions