如何使用环回解码“Content-Encoding:gzip,gzip”?
我创建了rest连接器以从后面的api获取响应:“https://rest.cricketapi.com/rest/v2/match/dev_season_2014_q3/balls/?access_token= ***************************** ****”。
我提到了许多网站,特别是https://github.com/strongloop/loopback/issues/1551但我没有得到解决方案。以下是代码:
/server/datasources.json
{
"Datasource": {
"host": "127.0.0.1",
"port": 27017,
"database": "dbname",
"name": "dbname",
"connector": "mongodb"
},
"cricketApi": {
"name": "cricketApi",
"baseURL": "https://rest.cricketapi.com/rest",
"crud": false,
"connector": "rest",
"operations": [
{
"functions": {
"auth": [
"app_id"
]
},
"template": {
"method": "POST",
"url": "https://rest.cricketapi.com/rest/v2/auth/",
"headers": {
"accepts": "application/json",
"content-type": "application/json"
},
"form": {
"access_key": "*********",
"secret_key": "**********",
"app_id": "{^app_id}",
"device_id": "********"
}
}
},
{
"functions": {
"ballbyball": [
"access_token"
]
},
"template": {
"method": "GET",
"url": "https://rest.cricketapi.com/rest/v2/match/dev_season_2014_q3/balls/",
"headers": {
"accepts": "application/json",
"content-type": "application/json"
},
"query": {
"access_token": "{access_token}"
}
}
},
{
"functions": {
"getships": []
},
"template": {
"method": "GET",
"url": "http://swapi.co/api/starships/",
"headers": {
"accepts": "application/json",
"content-type": "application/json"
}
}
},
{
"functions": {
"schedule": []
},
"template": {
"method": "GET",
"url": "https://rest.cricketapi.com/rest/v2/match/dev_season_2014_q3/balls/?access_token=*********",
"headers": {
"accepts": "application/json",
"content-type": "application/json",
"content-encoding": "gzip"
}
}
}
]
}
}
以下api正在运行但是压缩输出gzip:
{
"functions": {
"schedule": []
},
"template": {
"method": "GET",
"url": "https://rest.cricketapi.com/rest/v2/match/dev_season_2014_q3/balls/?access_token=2s151429438441649s961198289187901461",
"headers": {
"accepts": "application/json",
"content-type": "application/json",
"content-encoding": "gzip"
}
}
}
它提供以下输出:
“” \ u001f \ B \ u00006yZ \ u0002Mo0 \ FJS \ u001f(萨夫\ u0018P \ u0014,Q莫耳濏ӤY\ u0018uX {HS \ u0014 | L +Cn1ĵK;E7ʦF\ TB \ U0001 \ t8nS \ u0016%) E ZX a \u0015 $ ٱ hse k v > 7igu )#T } \u0003 \ ;; HMڀ\u0018˪ڮր\ F |; TUY @ \ u000f} \b�\\���\u0011\tC��\t)\u0016ED��\u0015�*\u0012Y\u001c�Ȩ�\u0007�J�ޞ�Lb\u0011�\u0010�:�8�r��qa\n�\u0011J�\u0010\u001b�\u0005)8\u0016�\u00178�\"°R�\u0011���\u0018�\"\u0013Z_\u0006\\�~�\b�\"N4Va(��\u0014ʂ�d*�3m��T�[�c�s�#�\u0005����2��p��\b\u0015�2c�3�ɂb%\"NM�\fU\f]N��z��6�����\u000f~���m�
JM; YQ> q d {c3iu \ u0017 \ [\ U0002 @릝\ NFU \ u0005 \ u001fzn;乌\ u000eh 5 \ u000fQ \ u001fV \u0000EӕphOO \ u0010 \˚F\ u0019ڱ\ n \ u000b /:\ u0007r \ u0001D \ BX \ u0006 / LB \ u0017 \ u0013N \ u0012 \ u003E \ u0015! Q/ z +!Ӯ Ӯ ' \ u0012>o \u0001 u媷\u0015Ͳ \ u0007} |Y \ u0000z / M SMYݣ7 \ u001b9H = U \ {RB AU \ u000b}; YYS [\ r \ u0004I ޢ} F的 \ n \ u000f \ tHg8j} HRG \ u0000的| \u0017Cζ{P҄\ u0000Jur(P߅\ u0015(թ \ u001c \ FOD \吨/ LN \ u000foO \ u001e>〜* F3 ^ X \ u0005L \ u001dcoys \ u001c | A和d \ u0004p_ \ u001e'b> b; CϳGG7'8β\ u001fOy \ u001a 7u ; / {\ u001e $Ё0< S
我还试图运行 DEBUG =压缩节点。来查看如何使用压缩。您可以查看下面给出的屏幕截图
我也尝试在server.js file =>
中使用压缩var compression = require('compression');
app.use(compression());
并在 /server/middleware.json 中添加以下行:
"compression": {
"params": { "threshold": 512 }
}
但它对我不起作用。
但是,当我使用以下代码正常调用它时,我知道它是如何工作的:
var http = require('http');
var request = require('request'), zlib = require('zlib');
var headers = {
'Accept-Encoding': 'gzip'
};
request({url:'https://rest.cricketapi.com/rest/v2/match/dev_season_2014_q3/balls/?access_token=2s151429438441649s96119828918*****', 'headers': headers})
.pipe(zlib.createGunzip()) // unzip
.pipe(process.stdout); //
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
如果我在环回休息连接器中获得解决方案,那将是非常有用的。谢谢!
答案 0 :(得分:2)
我瘦这个链接会帮助你...... Ryan Knell发布了这个答案
node.js - easy http requests with gzip/deflate compression
我们只需要添加" gzip:true"在我的请求中,因为请求已经支持
答案 1 :(得分:0)
终于得到了我的问题的解决方案:我创建了模型: /common/models/cricketapi.js 并编写以下代码:
'use strict';
module.exports = function(Cricketapi) {
// Ball By Ball API:
// @params: access_token and match_key
Cricketapi.ballbyball = function(access_token, match_key, cb){
var request = require('request');
request(
{
method: 'GET',
uri: 'https://rest.cricketapi.com/rest/v2/match/'+match_key+'/balls/?access_token='+access_token,
gzip: true
},
function (error, response, body) {
cb(null, JSON.parse(body));
}
)
};
Cricketapi.remoteMethod (
'ballbyball',
{
http: {path: '/ballbyball', verb: 'get'},
accepts: [
{arg: 'access_token', type: 'string', http: { source: 'query' }},
{arg: 'match_key', type: 'string', http: { source: 'query' }}
],
returns: {arg: 'response', type: 'Object'}
}
);
};
我得到以下输出:
我们需要添加" gzip:true"在请求中。