0

I'm trying to convert a working Lumen API service to AWS, and am stumped on getting an external REST API service to work. The service returns data compressed, but this fact isn't being passed through back to the app (Vue) in the browser properly. I tried adding in the headers in the response, as shown below, but it still isn't working. I can see the headers in the response in the browser console, but the browser still isn't interpreting it, so the data still looks like garbage. Any clues as to how to make this work?

var req = require('request');
exports.handler = function (event, context, callback) {
    const params = {
        url: 'http://api.service',
        headers: { 'Authorization': 'code',
                'Accept-Encoding': 'gzip,deflate',
                'Content-Type': 'application/json' },
        json: {'criteria': {
                    'checkInDate': '2019-10-22',
                    'checkOutDate': '2019-10-25',
                    'additional': {'minimumStarRating': 0},
                    'cityId': 11774}}
    };
    req.post(params, function(err, res, body) {
        if(err){
            callback(err, null);
        } else{
            callback(null, {
                "statusCode": 200,
                "headers": {
                    "Content-Type": "application/json",
                    "Content-Encoding": "gzip"
                },
                "body": body
            });
        }
    });
};
Michael Holland
  • 471
  • 3
  • 10
  • Did you check some of these answers? https://stackoverflow.com/questions/39453097/how-to-return-gzipped-content-with-aws-api-gateway – qkhanhpro Jun 17 '19 at 02:50
  • Yes, but my case is slightly different, in that I'm not trying to get the gateway to compress the data. It already is compressed from the API that Lambda calls. It would appear that Lambda isn't actually set up to handle getting compressed data, and therefore the interface between it and the API gateway has no facility to deal with it. – Michael Holland Jun 19 '19 at 00:56

1 Answers1

0

In the case you are seeing all scrambled characters, chance is you have not let API Gateway treat your Lambda answer as binary yet ( Since it is gzip-ed from your lambda )

Take a look at the document

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-configure-with-console.html

And this article

Unfortunately, the API Gateway is currently oblivious of gzip. If we’re using a HTTP proxy, and the other HTTP endpoint returns a gzipped response, it’ll try to reencode it, garbling the response.

We’ll have to tell the API Gateway to treat our responses as binary files — not touching it in any way.

https://techblog.commercetools.com/gzip-on-aws-lambda-and-api-gateway-5170bb02b543

qkhanhpro
  • 4,371
  • 2
  • 33
  • 45