0

I am using this API to consume a ESPN API:

http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6

In Node.js, using node-curl library, my snippet looks like this:

var Curl = require('node-curl/lib/Curl')
curl.setopt('URL', url);
curl.setopt('CONNECTTIMEOUT', 2);

curl.on('data', function(chunk) {
   console.log(chunk);
});

But everytime when I run this code I keep on getting response as:

<h1>596 Service Not Found</h1>

Strange is, same URL if I hit from the browser I get the correct response so URL is not invalid. This happens only if I try to call from Node.js. Can anyone guide me how to resolve this error? I tried encoding/decoding url, still I get same response.

Also basically I am avoiding any vendor specific libraries as much as possible, since we should have generic api calling framework

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107

1 Answers1

1

You could use request, which is a very popular module. Here's an example of how you could do it:

var request = require('request');

var url = 'http://api.espn.com/v1/now?apikey=d4skkma8kt2ac8tqbusz38w6';

request(url, function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(JSON.parse(body));
  }
});
Rodrigo Medeiros
  • 7,814
  • 4
  • 43
  • 54