Search code examples
javascriptaxioszlib

Error: incorrect header check at Zlib._handle.onerror (zlib.js:355:17) errno: -3, code: 'Z_DATA_ERROR'


My HTTP request has {'content-encoding': "gzip"} header. I am trying to read the data using node.js. I use below code to decompress the data. But i get Error: incorrect header check at Zlib._handle.onerror (zlib.js:355:17) errno: -3, code: 'Z_DATA_ERROR'. Can somebody help me to rectify this error.

var myHttp = require("http");
var url = require("url");
var qString = require("querystring");
var fs = require('fs');
var zlib = require('zlib'); 

var myEvents = require('./customEvents');

var myAppWebServer = myHttp.createServer(function(request, response){

    response.setHeader('Access-Control-Allow-Origin', 'http://localhost:8080');
    response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    response.setHeader('Access-Control-Allow-Credentials', true);
    response.setHeader('content-type', 'application/json');

    var body = "";

    request.on('data', function (chunk) {
        body += chunk;

    });

    request.on('end', function () {

        if(body){

          function getGzipped(url, callback) {

            var buffer = [];

            myHttp.get(url, function(response) {

                var gunzip = zlib.createGunzip(); 

                response.pipe(gunzip);

                gunzip.on('data', function(data) {

                    buffer.push(data.toString());

                }).on("end", function() {

                    callback(null, buffer.join("")); 

                }).on("error", function(e) {

                    callback(e);

                })
            }).on('error', function(e) {
                callback(e)
            });
        }

        getGzipped(url, function(err, data) {
               console.log(err);
            });
        }

      });


    response.end('{ "links" :"http://localhost:8080/users" }');

});

myAppWebServer.listen(8080);

Solution

  • deflate works well eventually!