如何ungzip(解压缩)NodeJS请求的模块gzip响应主体?

如何在请求的模块响应中解压缩压缩的正文?

我已经在网上尝试了几个示例,但似乎都没有用。

request(url, function(err, response, body) {

if(err) {

handleError(err)

} else {

if(response.headers['content-encoding'] == 'gzip') {

// How can I unzip the gzipped string body variable?

// For instance, this url:

// http://highsnobiety.com/2012/08/25/norse-projects-fall-2012-lookbook/

// Throws error:

// { [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' }

// Yet, browser displays page fine and debugger shows its gzipped

// And unzipped by browser fine...

if(response.headers['content-encoding'] && response.headers['content-encoding'].toLowerCase().indexOf('gzip') > -1) {

var body = response.body;

zlib.gunzip(response.body, function(error, data) {

if(!error) {

response.body = data.toString();

} else {

console.log('Error unzipping:');

console.log(error);

response.body = body;

}

});

}

}

}

}

回答:

我也无法获得工作请求,因此最终使用了http。

var http = require("http"),

zlib = require("zlib");

function getGzipped(url, callback) {

// buffer to store the streamed decompression

var buffer = [];

http.get(url, function(res) {

// pipe the response into the gunzip to decompress

var gunzip = zlib.createGunzip();

res.pipe(gunzip);

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

// decompression chunk ready, add it to the buffer

buffer.push(data.toString())

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

// response and decompression complete, join the buffer and return

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

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

callback(e);

})

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

callback(e)

});

}

getGzipped(url, function(err, data) {

console.log(data);

});

以上是 如何ungzip(解压缩)NodeJS请求的模块gzip响应主体? 的全部内容, 来源链接: utcz.com/qa/415403.html

回到顶部