Node.js 如何解码nodejs中的deflate字符串?
我正在使用nodejs构建一个应用程序,该应用程序使用一些外部API,这些API返回以deflate格式压缩的json数据,但由于我是nodejs新手,所以我查看了zlib API,但没有找到解码字符串的方法, 请任何人帮助我,这是我的解码字符串:Node.js 如何解码nodejs中的deflate字符串?,node.js,zlib,deflate,Node.js,Zlib,Deflate,我正在使用nodejs构建一个应用程序,该应用程序使用一些外部API,这些API返回以deflate格式压缩的json数据,但由于我是nodejs新手,所以我查看了zlib API,但没有找到解码字符串的方法, 请任何人帮助我,这是我的解码字符串: u�O��0\u0010ſJ�CN4��?RT���ޫʚ�\u0001����f)]��wذڪ�r��7�͓�W�\u0003���?_�\u0005T����\tT����\u000f����y=��cu\u0012��\u000e����A��,
u�O��0\u0010ſJ�CN4��?RT���ޫʚ�\u0001����f)]��wذڪ�r��7�͓�W�\u0003���?_�\u0005T����\tT����\u000f����y=��cu\u0012��\u000e����A��,5�\u0005\u0017ER�\u0011�^�8\rvE��0x��\u0000>��*�jTRA�\\SQe)/�d�����C�<?�4��4\u0007\b�\u001a��`(��@Y��\u0011{���-j���ք�\u0013� ^Y��T��\u001f��\u0010y�\u001a���=v�\'"�ʒGl��dX\'*��ӞR�b;��Y^���\u0017O{��\u001b��\u000fa��\u0018\u0014��8")\u0006m�\u001f�\u0007h����\u000ev96v�7G\u001f�vw��\t��J�i�U\u001a\u0018�(�7����Nζz@�G�p��iȲ,���\u000b\u0004po3��\u0018���\'i�`�(�S�]��P�Eˋ\u0013���\\�\u0007u�\nMЍ5\u0007w~�qhϜ�~E�\u0007OKp\u001f���l\u0003�\u0011~o\u000b����p\u0004m��X]�Uu�\u000b
u�O��0\u0010ſJ�CN4��?RT���ޫʚ�\u0001����f) ]��wذڪ�R��7.�͓�W�\u0003���?_�\u0005T����\tT����\u000f����y=��cu\u0012��\u000e����A.��,5.�\u0005\u0017ER�\u0011�^�8\rvE��0x��\u0000>��*�jTRA�\\SQe)/�D�����C� 嗯。。。首先你需要知道。。。API是否产生原始放气或放气。此外,在通缩时可以做出许多不同的选择。您必须了解放气时使用的配置。我假设您应该能够在正在使用的API文档中找到它们
现在让我们假设API正在使用默认设置()生成Deflate
现在
var zlib = require( 'zlib' );
// lets assume you have your response from api in a string variable
var responseDeflateString = "response from api";
// convert it to a buffer ( as you provided, its a utf8 string)
var responseDeflateBuffer = new Buffer( responseDeflateString );
// now inflate the buffer
// Note 1 : zlib.inflateSync takes another argument "option" and this will
// depend upon how this API service was deflating, and should be added
// based on API documentation. For now we are using default options.
// Note 2 : its better to use the asynchronous version. But we are using
// synchronous one for the sake of simplicity.
var responseInflateBuffer = zlib.inflateSync( responseDeflateBuffer );
// decode the buffer back to string ( utf8 )
var responseInflateString = responseInflateBuffer.toString();
支持gzip参数。退房申请
不使用任何第三方库,也不尝试检测服务器的编码:
var options = {
hostname: 'www.example.com',
port: 80,
path: '/deflateable-thing',
method: 'GET'
};
var req = http.request(options, function(res) {
deflate = zlib.createDeflate();
res.pipe(deflate);
deflate.on('data', function (chunk) {
console.log('data: ' + chunk.toString());
});
});
req.end();
标准库还提供了其他流处理程序,如gzip
。您可以在这里找到答案:谢谢@vanadium23,但它也没有帮助我。。
var options = {
hostname: 'www.example.com',
port: 80,
path: '/deflateable-thing',
method: 'GET'
};
var req = http.request(options, function(res) {
deflate = zlib.createDeflate();
res.pipe(deflate);
deflate.on('data', function (chunk) {
console.log('data: ' + chunk.toString());
});
});
req.end();