Reputation: 429
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
Reputation: 10246
This will might help you.
let request = require( 'request' ).defaults({rejectUnauthorized:false});
Upvotes: 7
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
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
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