Mike Malloy
Mike Malloy

Reputation: 1580

StackOverflow API not returning JSON

I am using a Node.js script to gather data from StackOverflow. I know that all responses are gzipped, so I put code in that should take care of that. My script is as follows:

var request = require('request');
var zlib = require('zlib');

function getStackOverflowResponse(url, callback){
  request(url, {encoding: null}, function(err, response, body){
      if(response.headers['content-encoding'] == 'gzip'){
          zlib.gunzip(body, function(err, dezipped) {
          callback(dezipped);
          });
      } else {
          callback(body);
      }
  });
}

var url = "https://api.stackexchange.com/docs/questions#pagesize=2&order=desc&min=2014-01-04&max=2014-02-02&sort=activity&tagged=apigee&filter=default&site=stackoverflow&run=true";

getStackOverflowResponse(url, function(questions) {
  console.log(questions);
});

Instead of getting the JSON output, I'm getting the following response:

Buffer 0d 0a 0d 0a 0d 0a 0d 0a 3c 21 44 4f 43 54 59 50 45 20 48 54 4d 4c 3e 0d 0a 3c 68 74 6d 6c 20 6c 61 6e 67 3d 22 65 6e 22 3e 0d 0a 3c 68 65 61 64 3e 20 0d ...

The response is enclosed in opening and closing angle brackets that I removed so that it would show up here.

Instead of callback(dezipped); I've tried callback(JSON.parse(dezipped)); and callback(JSON.parse(dezipped.toString()));

Nothing seems to be working for me. I still get the Buffer result regardless of what I do. Any help on how to make this work would be greatly appreciated.

Upvotes: 0

Views: 299

Answers (1)

brandonscript
brandonscript

Reputation: 73005

Joe's solution is the correct one - request is returning a buffer stream; converting it with toString() will solve the problem.

Also though, it looks like you're not actually calling the JSON endpoint (you're calling an HTML docs page?)

Try this:

var request = require('request');
var zlib = require('zlib');

function getStackOverflowResponse(url, callback) {
    request(url, {
        encoding: null
    }, function (err, response, body) {
        if (response.headers['content-encoding'] == 'gzip') {
            zlib.gunzip(body, function (err, dezipped) {
                callback(dezipped);
            });
        } else {
            callback(body);
        }
    });
}

var url = "https://api.stackexchange.com/2.1/questions?pagesize=2&order=desc&min=2014-01-04&max=2014-02-02&sort=activity&tagged=apigee&filter=default&site=stackoverflow&run=true";

getStackOverflowResponse(url, function (questions) {
    console.log(JSON.parse(questions.toString()));
});

Upvotes: 3

Related Questions