4

When I am trying to run the code from below:

var express = require('express');
var app = express();
var port = process.env.PORT || 5000;

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



app.listen(port, function() {
    console.log("Listening on " + port);
    makeRequest();
});



function makeRequest(){

    var url = 'https://api.stackexchange.com/2.1/search?pagesize=5&order=desc&sort=activity&intitle=ios development&site=stackoverflow';
    var headers = {'Accept-Encoding': 'gzip'};

    var response = request(url, headers);

    gunzipJSON(response);
}

function gunzipJSON(response){

    var gunzip = zlib.createGunzip();
    var json = "";

    gunzip.on('data', function(data){
        json += data.toString();
    });

    gunzip.on('end', function(){
        parseJSON(json);
    });

    response.pipe(gunzip);
}

function parseJSON(json){

    var json = JSON.parse(json);

    if(json.items.length){

        for(var i in json.items){

            console.log(json.items[i].title + '\n' + json.items[i].link);

        }

    }
}

I get error saying

Error: incorrect header check at Zlib._binding.onerror (zlib.js:286:17)

I am passing my own gzipped url in the code.

Any help will be really useful.

Thanks

Matt
  • 74,352
  • 26
  • 153
  • 180
user87267867
  • 1,409
  • 3
  • 18
  • 25

1 Answers1

0

Old question (and request library is now deprecated), but better late than never:

Interestingly, the code in question does work for me on Node.js version 15.13.0, but not on 14.16.0 (keeping the version of request the same, which is the latest 2.88.2).

However, just using the following simple code works on 14.16.0 (live demo), but not on 15.13.0!:

request(url, function (error, response, body) {
  console.log(JSON.parse(body)); 
});

This means that for some reason, on 14.16.0 the response body is automatically unzipped (hence the above snippet works), while on 15.13.0 the response body is kept compressed and so an active decompression is needed.

OfirD
  • 9,442
  • 5
  • 47
  • 90