1

I am struggling with getting Lambda + API gateway working well.

This is my code-

exports.handler = async (event) => {
    console.log("EVENT -> ", event.body)
    let buff = new Buffer(event.body, 'base64');
    let text = buff.toString('UTF-8');
    console.log("TEXT -> ",text)
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

I have referred to this answer, but, I don't see Integration Request in API Gateway.

here

What has to be done to get a proper JSON? I know I can use 3rd party npm libs. But, I prefer some fix at AWS end. I just want to use event.body which should return a JSON.

bozzmob
  • 12,364
  • 16
  • 50
  • 73

1 Answers1

1

I will re-intepret your question the way I understand It.

1/ You created a Lambda function, connected to a API gateway

2/ You sent an some data to the Lambda function using API gateway

3/ API gateway invoke your function with some data

4/ The data you received inside your Lambda function is neither lost nor corrupted. It is received inside Lambda as the Base64-Encoded format

5/ You do not want to receive the data in Lambda as Base64-Encoded, You do not want to do a step of decoding like you did

let buff = new Buffer(event.body, 'base64');
let text = buff.toString('UTF-8');

If that is the case, you need to take a look at this https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings-workflow.html

First, check what Content-Type header your request are sending.

Then, see if the data you sent to API gateway is actually textual data

Also, go to your API Gateway -> Setting -> Binary Media Types to see if any data type are specifically to be treated as Binary by API Gateway

qkhanhpro
  • 4,371
  • 2
  • 33
  • 45