Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect Header Check when using zlib in node.js

Tags:

node.js

zlib

I am trying to send a simple HTTP POST request, retrieve the response body.Following is my code. I am getting

Error: Incorrect header check

inside the "zlib.gunzip" method. I am new to node.js and I appreciate any help.

;

    fireRequest: function() {      var rBody = '';     var resBody = '';     var contentLength;      var options = {         'encoding' : 'utf-8'     };      rBody = fSystem.readFileSync('resources/im.json', options);      console.log('Loaded data from im.json ' + rBody);      contentLength = Buffer.byteLength(rBody, 'utf-8');      console.log('Byte length of the request body ' + contentLength);      var httpOptions = {         hostname : 'abc.com',         path : '/path',         method : 'POST',         headers : {             'Authorization' : 'Basic VHJhZasfasNWEWFScsdfsNCdXllcjE6dHJhZGVjYXJk',             'Content-Type' : 'application/json; charset=UTF=8',             // 'Accept' : '*/*',             'Accept-Encoding' : 'gzip,deflate,sdch',             'Content-Length' : contentLength         }     };      var postRequest = http.request(httpOptions, function(response) {          var chunks = '';         console.log('Response received');         console.log('STATUS: ' + response.statusCode);         console.log('HEADERS: ' + JSON.stringify(response.headers));         // response.setEncoding('utf8');         response.setEncoding(null);         response.on('data', function(res) {             chunks += res;         });          response.on('end', function() {             var encoding = response.headers['content-encoding'];             if (encoding == 'gzip') {                  zlib.gunzip(chunks, function(err, decoded) {                      if (err)                         throw err;                      console.log('Decoded data: ' + decoded);                 });             }         });      });      postRequest.on('error', function(e) {         console.log('Error occured' + e);     });      postRequest.write(rBody);     postRequest.end();  } 
like image 455
Thusitha Nuwan Avatar asked Oct 17 '13 23:10

Thusitha Nuwan


People also ask

What is use of zlib object in node JS?

The Zlib module provides a way of zip and unzip files.

What is Z_data_error?

Z_DATA_ERROR indicates that inflate() detected an error in the zlib compressed data format, which means that either the data is not a zlib stream to begin with, or that the data was corrupted somewhere along the way since it was compressed.

Is zlib stream duplex?

A duplex streams is both Readable and Writable. An example of that is a TCP socket. A transform stream is basically a duplex stream that can be used to modify or transform the data as it is written and read. An example of that is the zlib.


2 Answers

response.on('data', ...) can accept a Buffer, not just plain strings. When concatenating you are converting to string incorrectly, and then later can't gunzip. You have 2 options:

1) Collect all the buffers in an array, and in the end event concatentate them using Buffer.concat(). Then call gunzip on the result.

2) Use .pipe() and pipe the response to a gunzip object, piping the output of that to either a file stream or a string/buffer string if you want the result in memory.

Both options (1) and (2) are discussed here: http://nickfishman.com/post/49533681471/nodejs-http-requests-with-gzip-deflate-compression

like image 71
Nitzan Shaked Avatar answered Sep 24 '22 21:09

Nitzan Shaked


In our case we added 'accept-encoding': 'gzip,deflate' to the headers and code started working (solution credited to Arul Mani):

var httpOptions = {     hostname : 'abc.com',     path : '/path',     method : 'POST',     headers : {         ...         'accept-encoding': 'gzip,deflate'     } }; 
like image 20
Roman Avatar answered Sep 21 '22 21:09

Roman