1

I am just trying to send a get request to an API. I get a 200 response, but none of the callback functions seem to be happening, including end. Here is the relevant section, I also have the rest of the express server up, and the server is running.

var https = require("https");

https.get("https://tfe-opendata.com/api/v1/stops", function(res){
    console.log("Response: ", res.statusCode);
    console.log("Res: " + res);
}).on('data', function(data) {
    console.log('blah');
    console.log("data: " + data); 
    process.stdout.write(data);
}).on('error', function(error) {
    console.error('Error ' + error);
}).on('end', function(){
    console.log("End.");
});

It should just return JSON, and you can see the data and url are valid if you go to the address. None of the prints after the first callback happen.

Edit: It works if I use the full https.request method

var options = {
    host: 'tfe-opendata.com',
    path: '/api/v1/stops',
    method: 'GET'
}

var httpreq = https.request(options, function(response) {
    response.on('data', function(chunk) {
    console.log("data " + chunk);
    });
});

httpreq.on('error', function(e) {
    console.log('error: ' + e);
});

httpreq.end();

I still don't know why the original didn't work.

michaelAdam
  • 1,119
  • 14
  • 30
  • I am not really sure if you can use the path in the format of a domin `('https://tfe-opendata.com/api/v1/stops')`, perhaps you need to use an absolute path instead, something like `('/api/v1/stops')` – Ma'moon Al-Akash Jan 26 '15 at 13:10
  • You can see they do it in the documentation http://nodejs.org/api/https.html#https_https_get_options_callback – michaelAdam Jan 26 '15 at 13:14

2 Answers2

2

That's because you can't call .on('data', ) on the result of get, it has to be on the response instance inside of the callback.

A better alternative is probably just use a library that abstracts these low level details for you (such as request).

Ma'moon Al-Akash
  • 4,445
  • 1
  • 20
  • 16
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

You have been confused on using the API, you don't really call the on function on the object returned from https.get but instead you would do it from the res object provided from the callback function parameter.

The code speaks by it self, so instead of doing it like:

var https = require("https");

https.get("https://tfe-opendata.com/api/v1/stops", function(res) {
   console.log("Response: ", res.statusCode);
   console.log("Res: " + res);
}).on('data', function(data) {
   console.log('blah');
   console.log("data: " + data);
   process.stdout.write(data);
}).on('error', function(error) {
   console.error('Error ' + error);
}).on('end', function(){
   console.log("End.");
});

you should be doing it this way:

https.get("https://tfe-opendata.com/api/v1/stops", function(res) {
   console.log("Response: ", res.statusCode);
   console.log("Res: " + res);
   res.on('data', function(data) {
      console.log('blah');
      console.log("data: " + data);
      process.stdout.write(data);
   }).on('error', function(error) {
      console.error('Error ' + error);
   }).on('end', function(){
      console.log("End.");
   });
});
Ma'moon Al-Akash
  • 4,445
  • 1
  • 20
  • 16
  • Thanks to both of you. Small question: what is the difference between console.log and process.stdout.write ? – michaelAdam Jan 26 '15 at 13:28
  • Refer to the answer in this SO question http://stackoverflow.com/questions/4976466/difference-between-process-stdout-write-and-console-log-in-node-js – Ma'moon Al-Akash Jan 26 '15 at 13:31