how to read API gateway body into json in lambda?

Viewed 657

I have an AWS API gateway function connected to the following lambda function:

exports.handler = async (event,context) => {   
     
    let buff = new Buffer(event.body,"base64");
    let data = buff.toString("ascii");
    

    const response = {
        statusCode: 200,
        body: JSON.stringify(data + ' Hello from Lambdsa!'),
    };
    return response;
};

It's a POST function and the current return is:

"----------------------------866060539566167157861191\r\nContent-Disposition: form-data; name=\"email\"\r\n\r\nmyemail.dk\n\r\n----------------------------866060539566167157861191--\r\n Hello from Lambdsa!"

How can I make this base64 decoded content into JSON I can read more easily in my lambda function with the HTTP API?

1 Answers

If you only want to have body in your lambda function and let request and response transformation to be managed by API Gateway, then you can simply go to API Gateway settings of this lambda function and disable Use Lambda Proxy Integration option.

Here is the quick graphic representation of difference between using and not using it :

enter image description here

Also, attaching a screenshot where you would find the option to disable it :

enter image description here

Note: Removed some sensitive info.

Now you simply get the body in lambda function as :

exports.handler = async (body) => {
  try {
     // Do some operation here
      return body;
  } catch (error) {
      return error;
  }
};
Related