node.js - StackOverflow API not returning JSON -


i using node.js script gather data stackoverflow. know responses gzipped, put code in should take care of that. script 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 json output, i'm getting 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 enclosed in opening , closing angle brackets removed show here.

instead of callback(dezipped); i've tried callback(json.parse(dezipped)); , callback(json.parse(dezipped.tostring()));

nothing seems working me. still buffer result regardless of do. on how make work appreciated.

joe's solution correct 1 - request returning buffer stream; converting tostring() solve problem.

also though, looks you're not calling json endpoint (you're calling 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())); }); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -