user3024827
user3024827

Reputation: 1258

AWS Lambda POST

I have a POST method in which will be used to add comments to my RDS using Lambda. i am trying to connect it all up using the AWS API, however can't figure out how to do so.

How can I read the body of my HTTP request (specifically the comment variable) into my lambda function.

{"body" : $input.json('$')}

Will that take the whole POST body and make it available in the Lambda function?

Upvotes: 2

Views: 1340

Answers (1)

Lokesh Bhatt
Lokesh Bhatt

Reputation: 17

I have added few line of code in my lambda function to get all the header,body data.

Step-:

  • Go to Integration Request of the API created for the lambda function.
  • Add mapping template and paste below code there

    {
  "body" : $input.json('$'),
  "headers": {
    #foreach($header in $input.params().header.keySet())
    "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end

    #end
  },
  "method": "$context.httpMethod",
  "params": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "query": {
    #foreach($queryParam in $input.params().querystring.keySet())
    "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end

    #end
  }  
}

Now in our event parameter you can get body data as event.body and header data as event.headers

Upvotes: 1

Related Questions