Node.js Https request error

I tried a sample from the documentation and it works great.

But when I change the URL to https://api.mercadolibre.com/sites/ , the request freezes. The only thing I get is:

 { [Error: socket hang up] code: 'ECONNRESET' } 

Here is my code:

 var https = require('https'); this.dispatch = function(req, res) { var renderHtml = function(content) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(content, 'utf-8'); } var parts = req.url.split('/'); var options = { host: 'api.mercadolibre.com', port: 443, path: '/sites/', method: 'GET' }; var request = https.request(options, function(res) { console.log("statusCode: ", res.statusCode); console.log("headers: ", res.headers); res.on('data', function(d) { process.stdout.write(d); }); }); request.on('error', function(e) { console.error('error'); console.error(e); }); request.end(); return 'item id:' + parts[2]; }; 

I tried with curl, soapui and with a browser. In all cases it works fine, but with node.js it is not.

How can I get more data about what is happening?

added

With curl, I do: curl --sslv3 https://api.mercadolibre.com/sites/ works. I am testing the same thing in centos 6 and it works too. I reinstalled node, this time from the original, same problem. My Os - ubuntu 12.04. Thanks.

+6
source share
6 answers

I'm not sure about the api.mercadolibre.com site, but I can call the API if I remove the port parameter, for example the following code:

 var options = { host: 'api.mercadolibre.com', path: '/sites/', method: 'GET' }; 

And we also need to add an option to support SSL version 3:

 https.globalAgent.options.secureProtocol = 'SSLv3_method'; 
+7
source

Since it works with curl, try using the node -curl module. I lost all day trying to get it to work in node.js with http and / or https modules until I switch to node -curl.

Try the following:

 var curl = require('node-curl'); curl('https://api.mercadolibre.com/sites/', {SSLVERSION: 3}, function(err, res) { var body = res.body; res.close(); console.log(body); }); 
+3
source

Why not use a library like request to figure out the details for you?

 var request = require('request'); request('https://api.mercadolibre.com/sites/', {}, function(err, res, body) { console.log("Got body: ", body); }); 

This gives:

 Got body: [{"id":"MLA","name":"Argentina"},{"id":"MLB","name":"Brasil"},{"id":"MCO","name":"Colombia"},{"id":"MCR","name":"Costa Rica"},{"id":"MEC","name":"Ecuador"},{"id":"MLC","name":"Chile"},{"id":"MLM","name":"Mexico"},{"id":"MLU","name":"Uruguay"},{"id":"MLV","name":"Venezuela"},{"id":"MPA","name":"Panamรก"},{"id":"MPE","name":"Perรบ"},{"id":"MPT","name":"Portugal"},{"id":"MRD","name":"Dominicana"}] 
+2
source

Same thing here, working with curl, but not with node.js.

Problem: here in CentOS-5, curl usesthe provides openssl libraries and therefore uses the standard centos / etc / pki / tls / certs / ca-bundle.crt for CA checks.

Where to search for node.js ?, through strace there I do not see any link to the CA file for verification. Node.js a server request with a valid SSL certificate from a well-known old issuer was accepted, but not against my own web server with its own CA. I put my CA.crt in the ca-bundle.crt file, so now curl accepts it, but not node.js.

The only solution at the moment is to deactivate check-check for my dev-box:

  var client = require('https'); var download_options = url.parse(sourceUrl); download_options.method = "GET"; download_options.agent = false; download_options.rejectUnauthorized = false; / HERE to accept all SSL-certificates */ var download_request = client.request(download_options); 
+1
source

I think that you are behind the proxy server that you need to specify for the request. Proxy settings are automatically determined using libcurl, which uses node -curl. Therefore, the request passes in node -curl.

Therefore, find out the IP address of the proxy server and its organization, and try the following:

 var request = require('request'); request({ uri : 'https://mail.google.com/mail', proxy : 'http://<proxy ip>:<proxy port>' }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Print the google web page. }else{ console.log(error); console.log(response.statusCode); } }) 
0
source

You will get an ECONNRESET error if you do this:

 post_options.path = 'history'; ... var req = http.request(post_options, function(res) { ... 

That is, you need to make sure your path has / as follows:

 post_options.path = '/history'; ... 
0
source

Source: https://habr.com/ru/post/916772/


All Articles