Amuk Saxena
Amuk Saxena

Reputation: 1571

Loopback API: Response getting stuck at compression

How to decode “Content-Encoding: gzip, gzip” using loopback?

I have created rest connector to get response from following api : "https://rest.cricketapi.com/rest/v2/match/dev_season_2014_q3/balls/?access_token=*********************************".

I referred to many sites and specially https://github.com/strongloop/loopback/issues/1551 But I am not getting the solution. Following is the code:

/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"
          }
        }
      }
    ]
  }
}

Following api is working but giving output gzip compressed:

{
    "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"
      }
    }
  }

it is giving the following output:

enter image description here

""\u001f�\b\u00006�yZ\u0002��Mo�0\f��J�s���\u001f(z�a�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ӕ��ph��ٰOO\u0010\f�\u0019ڱ\n�\u000b/:\u0007�r\u0001D�\bx\u0006/Lb��\u0017�\u0013N\u0012�\u0003E\u0015!�Q/z+!Ӯ��ڗ�'�\u0012 >o��\u0001�u媷\u0015Ͳ�\u0007}¦Y����\u0000z/�M����smyݣ�7�\u001b9H=�u\rb���{�aU���\u000b}�;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��o�ys\u001c|��a�&D\u0004p_\u001e�'�b�>b��;��c��ؓϳGG�7�'�8�β��\u001f�����Oy\u001a���7u��;/{\u001e$Ё0< S

I have also tried to run DEBUG=compression node . to see how compression is used. You can have a look on the screen shot given below

enter image description here

I also tried using compression in server.js file =>

var compression = require('compression');
app.use(compression());

and in /server/middleware.json added following lines:

"compression": {
  "params": { "threshold": 512 }
}

But its doesn't work for me.

However I know how it works when I call it normally with the following code:

    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); 

It will be great and helpful if I get the solution in loopback rest connector . Thanks!

Upvotes: 0

Views: 1240

Answers (2)

Amuk Saxena
Amuk Saxena

Reputation: 1571

Finally got the solution to my question: I created model: /common/models/cricketapi.js and write the following code:

'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'}
    }

  );
};

I am getting the following output:

enter image description here

we need to add "gzip: true" in request.

Upvotes: 1

radhey shyam
radhey shyam

Reputation: 769

I thin this link will help you... Ryan Knell posted this answer

node.js - easy http requests with gzip/deflate compression

we only need to add "gzip: true" in my request, because request already supports

https://github.com/request/request

Upvotes: 2

Related Questions