0

I'm trying to make a request to here, if you click on the link you should see a JSON response (as expected). I've, tried https and http, it doesnt matter (at least I don't think so).

Anyways the problem when I try to get a response from the command line, I get non UTF-8 characters like �������B��������E��9 as a response, even when I specify utf-8 encoding. Ive tried the npm module request and doing node http/https requests.

All i need is to just return a JSON response.

I've also tried JSON.parse() but to no avail.

Here's the code I've tried

var request = require("request")

var url = endpoint;

request({
    url: url,
    json: true
}, function (error, response, body) {

if (!error && response.statusCode === 200) {
    console.log(body); // Print the json response
}
})

and the basic http request

var endpoint = 'http://api.stackexchange.com/2.1/search/advanced?order=desc&sort=relevance&q=jsonp&site=stackoverflow';
var req = http.request(endpoint, function(res) {
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
Jackson Geller
  • 189
  • 1
  • 1
  • 10
  • reason for https: stackoverflow api is https, but http works fine. – Jackson Geller Jan 05 '14 at 07:07
  • You can try add `accept-encoding: ''` to request headers, if it not work you should decode gzip like this: http://stackoverflow.com/questions/8880741/node-js-easy-http-requests-with-gzip-deflate-compression – damphat Jan 05 '14 at 07:37

1 Answers1

0

Stackoverflow servers are misconfigured, so they return gzip-encoded body even though you didn't ask for it.

Looks like you have to gunzip your response after receiving it.

alex
  • 11,935
  • 3
  • 30
  • 42
  • `curl 'http://api.stackexchange.com/2.1/search/advanced?order=desc&sort=relevance&q=jsonp&site=stackoverflow' | gunzip -` works fine for me – alex Jan 05 '14 at 07:45