Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding issue with requesting JSON from StackOverflow API

I can't figure this out for the life of me. Below is an implementation with the request module, but I've also tried with the node-XMLHttpRequest module to no avail.

var request = require('request');

var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';

request.get({ url: url }, function(error, response, body) {
    if (error || response.statusCode !== 200) {
        console.log('There was a problem with the request');
        return;
    }

    console.log(body); // outputs gibberish characters like �
    console.log(body.toString()); // also outputs gibberish
});

Seems to be an encoding issue, but I've used the exact same code (with native XHR objects) in the browser and it works without problems. What am I doing wrong?

like image 576
Jason Barry Avatar asked Feb 10 '13 00:02

Jason Barry


1 Answers

The content is gzipped. You can use request and zlib to decompress a streamed response from the API:

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

var url = 'http://api.stackexchange.com/2.1/questions?pagesize=100&fromdate=1356998400&todate=1359676800&order=desc&min=0&sort=votes&tagged=javascript&site=stackoverflow';

request({ url: url, headers: {'accept-encoding': 'gzip'}})
  .pipe(zlib.createGunzip())
  .pipe(process.stdout);  // not gibberish

Further Reading: Easy HTTP requests with gzip/deflate compression

like image 52
Pero P. Avatar answered Sep 20 '22 23:09

Pero P.