Gotenks
Gotenks

Reputation: 377

GET Request in node.js is not working

I'm having big troubles loading a page through Node.js. This is the code of my Node.js Application, please note it works if I try to load other resources (like www.google.com) so the question is specific to the domain name www.egopay.com:

 var http = require('http');
 var options = {
    hostname: 'www.egopay.com',
    port: 80,
    path: '/',
    method: 'GET'
};

var req = http.request(options, function(res) {
    console.log('STATUS: ' + res.statusCode);
    console.log('HEADERS: ' + JSON.stringify(res.headers));
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

Using cURL it works, Node.js won't.

Around one minute after the request this error shows up:

problem with request: read ECONNRESET

Upvotes: 1

Views: 5725

Answers (2)

MobA11y
MobA11y

Reputation: 18860

When I made the same request, but added a user-agent to the headers, I got a response, that made it clear what was happening. Try adding this line to your options:

headers: {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36'},

Basically, they have an nginx server that redirects you to an HTTPS site with the same URL. Your not getting a response without including a user agent is probably a bug in the nginx deployment for that site. I can explain it no other way, as I cannot recreate the circumstances in Chrome or Firefox. Even when overriding with an empty user-agent. I imagine it is the difference between an empty string and an undefined string. Node is sending the header's field as completely undefined, whereas the browsers I'm trying to replicate with are only sending an empty string. Again, can't recreate, but this is my best guess.

Upvotes: 3

Jonathan Crowe
Jonathan Crowe

Reputation: 5803

I had similar issues with requests in Node js so i wrote this npm package:

https://npmjs.org/package/curljs

Hope it helps!

Upvotes: 2

Related Questions