简体   繁体   中英

Http request to stackexchange api returns unreadable json

I'm trying to fetch some json data from the stackexchange api. Receiving the OAuth code and access token works fine. But when calling the actual datafetching endpoints, the response does look wierd. Probably encoded or similar.

The request looks like this:

var client = requestjson.newClient('https://api.stackexchange.com');
    client.get("/2.2/me/comments?order=desc&sort=creation&site=stackoverflow&access_token="+myToken+"&key="+key, function(err, res, body) {
        console.log(body);
    })

And then the response body looks like this:

i�)�)QEJ�a��Ml�d4���20�c����M���]�v5/AZ�m��z    �C��`�~���*ͳ`Fh'����<M��k��J������J��>       &��ȗ����m��o>U�n�鴬�x=M��}1��m��'����ϻ��#
��zDn���n=ϳh[��QY��M���uv�*����&?;��S��х�V���'{mJ?  �8/�W�q���͓��+��qK��������X�9X~��g�������΁YrVY���B���X1#�`E

I've tried JSON.parse but it throws an error in the console.

Found the answer myself here: node.js - easy http requests with gzip/deflate compression

Added the following:

var reqData = {
    url: "https://api.stackexchange.com/2.2/me/comments?order=desc&sort=creation&site=stackoverflow&access_token="+myToken+"&key="+key,
    method:"get",
    headers: {'Accept-Encoding': 'gzip'}
}
var gunzip = zlib.createGunzip();
var json = "";
gunzip.on('data', function(data){
    json += data.toString();
});
gunzip.on('end', function(){
    console.log(JSON.parse(json));
});
request(reqData)
    .pipe(gunzip)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM