5

I know how to access GET string variables in a Node js Lambda that is integrated with API Gateway with:

 event["queryStringParameters"]["variable_name"]

What is the equivalent for accessing POST variables?

4b0
  • 21,981
  • 30
  • 95
  • 142
jaimerr
  • 180
  • 2
  • 11

1 Answers1

15

Use the following

if (event.body !== null && event.body !== undefined) {
        let body = JSON.parse(event.body) //use in case of JSON body
        //your code
}

AWS Documentation

Sandeep Singh
  • 745
  • 4
  • 20
  • I'm unsure why this answer was downvoted, but it is correct. When you receive a POST in your Lambda handler from API Gateway, the Lambda event contains a field called `body` which contains a JSON string representation of the POST body. You can parse the JSON into an object and access your POST variables as keys in the body object. – Joe Lafiosca Mar 15 '18 at 16:31
  • Thx. Json parse didn't work for me but the rest got me in the right track – jaimerr Mar 23 '18 at 20:09
  • @jaimerr you need to make sure your body is valid in json format – anhduc.bkhn Jan 07 '19 at 08:19
  • Your body will be whatever you post to it. If you post multipart form data, JSON parse will tank. That's probably why it is getting downvoted. – Saeven Aug 17 '20 at 14:31
  • I am using JSON. parse & there is no multi-part data. event. the body is resulting in an unexpected JSON. – Preet Saxena Feb 27 '21 at 19:34
  • 1
    @PreetSaxena Above code example is for when the posted body is in JSON format. You can get an idea about multipart data [here](https://stackoverflow.com/questions/41756190/api-gateway-post-multipart-form-data) – Sandeep Singh Mar 01 '21 at 09:40
  • Thank you, it was working. I was expecting that it will work the same as mock events but it didn't so we need to follow the multipart concept. – Preet Saxena Mar 13 '21 at 23:49