环回 API:响应卡在压缩状态
Loopback API: Response getting stuck at compression
如何使用环回解码“Content-Encoding: gzip, gzip”?
我创建了休息连接器以从以下 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\u00006�yZ\u0002�Mo�0\f�J�s��\u001f(z�a�v\u0018P\u0014�,˩Q��l�M�濏�ӤY��\u0018�uX{H S\u0014��|l+�C�n١ĵK;E7�ʦF\tb\u0001\t8�n� S��\u0016%��)��E��ZX����a��\u0015��$ ����ٱ��hse��k������v��>��7igu��)#T���� }��\u0003��\;��;��hmˊ\u0018��˪��ڮ��ր����\f|��;T��uY��@\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�
�� J�m;YQ>q�d{����c3i�u\u0017����\[����\u0002�@��?릝�\nf����U\u0005\u001f �zn;O�o\u000eh�5\u000fQ\u001fV�\u0000�E� 78=]�\bx\u0006/Lb�\u0017�\u0013N\u0012�\u0003E\u0015!�Q/z+!� \u0001... }��;YYs[\r����\u0004��I����}]��f��\n��\u000f\t����H��g�� 8j��}��HrG����\u0000|\u0017Cζ����{��P������\u0000Jur(��P������\u0015(թ��\u001c��\f�OD\t/lN\u000f��oO�\u001e>~*��F��3^��X\u0005L\u001d������c��oys\u001c|��a��&D\u0004p_\u001e��'b��>b��;��c��ΓρGG��7��8��β��\u001f������Oy\u001a����7u��;/ {\u001e$Ё0< S
我也试过 运行 DEBUG=compression node . 看看压缩是如何使用的。您可以查看下面给出的屏幕截图
我也尝试在 server.js 文件中使用压缩 =>
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);
如果我在 loopback rest connector 中得到解决方案,那将是非常有用的。谢谢!
我觉得这个 link 会帮助你...
Ryan Knell 发布了这个答案
我们只需要在我的请求中添加"gzip: true",因为请求已经支持
终于找到了我的问题的答案:我创建了模型:
/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"。
如何使用环回解码“Content-Encoding: gzip, gzip”?
我创建了休息连接器以从以下 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\u00006�yZ\u0002�Mo�0\f�J�s��\u001f(z�a�v\u0018P\u0014�,˩Q��l�M�濏�ӤY��\u0018�uX{H S\u0014��|l+�C�n١ĵK;E7�ʦF\tb\u0001\t8�n� S��\u0016%��)��E��ZX����a��\u0015��$ ����ٱ��hse��k������v��>��7igu��)#T���� }��\u0003��\;��;��hmˊ\u0018��˪��ڮ��ր����\f|��;T��uY��@\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�
�� J�m;YQ>q�d{����c3i�u\u0017����\[����\u0002�@��?릝�\nf����U\u0005\u001f �zn;O�o\u000eh�5\u000fQ\u001fV�\u0000�E� 78=]�\bx\u0006/Lb�\u0017�\u0013N\u0012�\u0003E\u0015!�Q/z+!� \u0001... }��;YYs[\r����\u0004��I����}]��f��\n��\u000f\t����H��g�� 8j��}��HrG����\u0000|\u0017Cζ����{��P������\u0000Jur(��P������\u0015(թ��\u001c��\f�OD\t/lN\u000f��oO�\u001e>~*��F��3^��X\u0005L\u001d������c��oys\u001c|��a��&D\u0004p_\u001e��'b��>b��;��c��ΓρGG��7��8��β��\u001f������Oy\u001a����7u��;/ {\u001e$Ё0< S
我也试过 运行 DEBUG=compression node . 看看压缩是如何使用的。您可以查看下面给出的屏幕截图
我也尝试在 server.js 文件中使用压缩 =>
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);
如果我在 loopback rest connector 中得到解决方案,那将是非常有用的。谢谢!
我觉得这个 link 会帮助你... Ryan Knell 发布了这个答案
我们只需要在我的请求中添加"gzip: true",因为请求已经支持
终于找到了我的问题的答案:我创建了模型: /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"。