Getting incomplete response from GitHub API

164 views Asked by At

I'm trying to learn nodejs and have this page which is supposed to print out all of the repos that someone has at GitHub. Right now, it randomly stops halfway through the message, so if I try to parse it into JSON it fails.

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  var https = require('https');
  var options = {
    host: 'api.github.com',
    headers: {'user-agent': 'GitHubAPI'},
    path: '/users/vermiculus/repos'
  };
  var gitapi = https.get(options, function(git) {
    git.on("data", function(chunk) {
      //JSON.parse(chunk); //DEBUG Fails and dies.
      res.end(chunk);
    });
  });
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Edit: Here's a sample response. See how it literally stops in the middle of a string.

[{"id":24659454,"name":"arara","full_name":"vermiculus/arara","owner":{"login":"vermiculus","id":2082195,"avatar_url":"https://avatars.githubusercontent.com/u/2082195?v=3","gravatar_id":"","url":"https://api.github.com/users/vermiculus","html_url":"https://github.c
2

There are 2 answers

4
robertklep On BEST ANSWER

The data event for the response object of https.get() can get called multiple times, but you're ending your HTTP server's response after the first.

You should collect all the chunks and combine them in an end handler:

var gitapi = https.get(options, function(git) {
  var chunks = [];

  git.on("data", function(chunk) {
    chunks.push(chunk);
  });

  git.on("end", function() {
    var json = Buffer.concat(chunks);
    res.end(json);
  });
});

FWIW, if the GH API data is JSON, setting text/plain as content type doesn't make a lot of sense.

If you just want to proxy the GH responses, you can also use pipe() (which basically does the same as above, just more efficiently):

var gitapi = https.get(options, function(git) {
  git.pipe(res);
});
0
Bidhan On

You're using res.end after you get a chunk of data. Try to concatenate all the chunks into one single string before parsing it.

var gitapi = https.get(options, function(git) {
var body = '';
git.on("data", function(chunk) {
  body += chunk.toString('utf8');
});
git.on("end", function() {
   //var json = JSON.parse(body)
   res.end(body);
});
});