0

I have a use case where when a user hits my API gateway I spawn a Lambda. The Lambda is using aws serverless express.

The Lambda has the responsibility to fetch the data from a url. Lets say this url is tiny url which is amazons url.

I am fetching the data. My code to fetch the data is

function (req, res) {
    var request1 = require('request')
    res.setHeader('Content-Type', 'text/html; charset=utf-8');
    request1.get('http://a.co/d/dBpaoQo')
        .on('response', response => {
            response.pipe(res);
        });

}

However, when I see the data at the client end, I see the data as

��P_k�0}ϧH}� U�e輹{X�`���� ���1�\��bbmQ��]���p����CZ/��^w�q�F�����C�Ν�gy���I�����!�h�<T��s0�W����r��ko7���j�ȓ�ΊyF<��<��z�hp+�L�&�s%:| F�l/f٫.��*���z�2����0���JGD��8A��W��1�=�S��3����˜m.�Do~޼�|s{����O�����G���-2;O�h�;^�����4�$�D?&���F֊�w����Џ~�\�P��0,Z�7´�Z ��s��g�\��c��ڿ�N�M���>�^�,Ka?��,�'nd�H�Z]�����\���@s��2������#� ra#N<��V�

(Not Pasting the whole content).

If I run the server locally it shows the data in correct format. It is basically API gateway which is messing up with the data. I even applied

res.setHeader('Content-Type', 'text/html; charset=utf-8')

in my code as suggested on stackoverflowlink

Can anyone point me what am I doing wrong.

Aditya Sharma
  • 389
  • 3
  • 11

1 Answers1

0

Ok this was an issue with Lambda and not with using API-Gateway. When I was fetching the data it was a compressed file.

I had to put in request headers(request where I am requesting the URL to give me data)

Accept-Encoding: 'none'

However, I did not understand why it is working locally and not on Lambda. When I run the same code locally I get the correct file.

Aditya Sharma
  • 389
  • 3
  • 11