问题
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);
回答1:
I was seeing the same error trying to POST from the client side. If the sender is using zlib on data buffers then the encoding and header will be "zlib" not "gzip".
I changed "Content-Encoding" from "gzip" to "deflate" (a.k.a. zlib; see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding for details).
The server started accepting my POSTs. So your problem may be on the sending end rather than the decoding.
来源:https://stackoverflow.com/questions/46190427/error-incorrect-header-check-at-zlib-handle-onerror-zlib-js35517-errno-3