richwednesday
richwednesday

Reputation: 3

ECONNRREFUSED Error when making a get request to an API with Nodejs

I did make a get request to an API but get an ECONNREFUSED Error. The problem is not the API because when I type it in a browser, I get back results in JSON.

This is my code;

var https = require("https");

var options = {
    host :  'nairabox.com',
    port : 443,
    path : '/v1/tickets/auth=APIKEY&as=showtimes&cinemaId=CINEMAID&ticketId=TICKETID',
    method : 'GET'
}


var req = https.request(options, function(res) {
  res.on('data', function(data) {
      console.log( JSON.parse(data));
  });
});

req.end();

req.on('error', function(err){
    console.log("Error: ", err);
}); 

This is the error;

Error:  { Error: connect ECONNREFUSED 162.255.119.75:443
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '162.255.119.75',
  port: 443 }

Anyone can be kind enough to test it replacing the parameters with random numbers and you'd get the same error. How can I fix it. API gives results in the browser though.

Upvotes: 0

Views: 95

Answers (1)

robertklep
robertklep

Reputation: 203419

The hostname nairabox.com resolves to two IP-numbers, 178.79.175.127 and 162.255.119.75. The latter is refusing connections, which is the issue you're running in to.

However, the host www.nairabox.com resolves to only one IP-number, 178.79.175.127, so I guess you should be using that hostname instead of the one without the www. prefix.

Upvotes: 1

Related Questions