本文介绍了如何解压缩(解压缩)NodeJS请求的模块gzip响应体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在Web上尝试了几个例子,但没有一个看起来正常工作。
request(url,function(err,response,body){
if(err){
handleError )
} else {
if(response.headers ['content-encoding'] =='gzip'){
//如何解压gzipped的字符串body变量?
//例如,这个url:
// http://highsnobiety.com/2012/08/25/norse-projects-fall-2012-lookbook/
//抛出错误:
// {[错误:错误的头部检查] errno:-3,代码:'Z_DATA_ERROR'}
//但是,浏览器显示页面精细,调试器显示其gzip压缩
//并且由浏览器解压缩$ ...
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);
函数getGzipped(url,callback){
//缓存来存储流式解压缩
var buffer = [];
http.get(url,function(res){
//将响应导入gunzip解压缩
var gunzip = zlib.createGunzip();
res.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(data);
});
How do I unzip a gzipped body in a request's module response?
I have tried several examples around the web but none of them appear to work.
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;
}
});
}
}
}
}
解决方案
I couldn't get request to work either, so ended up using http instead.
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);
});
这篇关于如何解压缩(解压缩)NodeJS请求的模块gzip响应体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!