Question

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?

Was it helpful?

Solution

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

OTHER TIPS

While pero's answer is correct, there's a simpler way to do this.

Since you're using request, you can also just add the gzip: true flag:

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, headers: {'accept-encoding': 'gzip'}, gzip: true }, function(error, response, body) {
    console.log(body); // not gibberish
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top