简体   繁体   English

如何强制Node的请求模块不对响应进行编码?

[英]How can I force Node's request module to not encode the response?

I'm using Node's request module. 我正在使用Node的request模块。
The response I get is "gziped" or otherwise encoded. 我得到的反应是“gziped”或以其他方式编码。

How can I 我怎样才能
1. Build the request to not encode the response? 1.构建不对响应进行编码的请求?
2. Decode the response? 2.解码响应?
The data is coming from http://api.stackexchange.com . 数据来自http://api.stackexchange.com

var myRequest = require('request');
var zlib = require('zlib');

var stackRequest = require('request'); 
var apikey = '<MyKey>';
var fromdate = '1359417601';
var tagged = 'node.js';
stackRequest(
    { method: 'GET'
        , uri: 'http://api.stackexchange.com/2.1/questions?key=' + apikey + 
          '&site=stackoverflow&fromdate=' + fromdate + '&order=desc&' + 
          'sort=activity&tagged=' + tagged + '&filter=default'
}, function(err, response, body) { 
    console.log(response.body); // How can I decode this?
}); 

The encoding has nothing to do with request . 编码与request无关。 StackOverflow's API returns GZip encoded data always, as explained in the API documentation . StackOverflow的API始终返回GZip编码数据,如API文档中所述 You need to use Node's zlib module to unzip the contents. 您需要使用Node的zlib模块解压缩内容。 This is a simple example: 这是一个简单的例子:

var zlib = require('zlib');

// Other code

, function(err, response, body) {
   zlip.gunzip(body, function(err, data){
     console.log(data);
  });
});

The main downside of this, which is bad, is that this forces the request module to process the entire response content into one Buffer as body . 这样做的主要缺点是,这会强制request模块将整个响应内容作为body处理到一个Buffer中。 Instead, you should normally use Node's Stream system to send the data from the request directly through the unzipping library, so that you use less memory. 相反,您通常应该使用Node的Stream系统直接通过解压缩库从请求中发送数据,以便您使用更少的内存。 You'll still need to join the parts together to parse the JSON, but it is still better. 您仍然需要将这些部分连接在一起来解析JSON,但它仍然更好。

var zlib = require('zlib');
var request = require('request');

var apikey = '<MyKey>';
var fromdate = '1359417601';
var tagged = 'node.js';
var compressedStream = request('http://api.stackexchange.com/2.1/questions?' +
    'key=' + apikey + '&site=stackoverflow&fromdate=' + fromdate +
    '&order=desc&sort=activity&tagged=' + tagged + '&filter=default');

var decompressedStream = compressedStream.pipe(zlib.createGunzip());

var chunks = [];
decompressedStream.on('data', function(chunk){
  chunks.push(chunk);
});
decompressedStream.on('end', function(){
    var body = Buffer.concat(chunks);

    var data = JSON.parse(body);

    // Do your thing
});

First set accept: identity as a header. 首先接受:身份作为标题。 If stacked change doesn't send data as regular UTF8, then it's a bug on their end. 如果堆叠更改不像常规UTF8那样发送数据,那么它就是一个错误。

Secondly, you want to set the encoding as UTF8 so the response isn't a buffer. 其次,您希望将编码设置为UTF8,因此响应不是缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM