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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With