1
votes

I know how to set up API Gateway and link it to a Lambda function. What I don't know, and God have I searched, is how to get the response body from API Gateway in Lambda.

How do I get it? I see it when I test the API in the AWS console.

1
Do you mean the get query parameters of the url and/or body parameters from the URL request to be accessed in the lambda function?Yan
I'd like to know how to do bothfroinds

1 Answers

4
votes

I think you have to setup Body Mapping Template. Goto Gateway API -> API That you created -> Resources -> Method(Get/Post .. ) -> Integration Request -> Body Mapping Template -> Add Mapping Template Content Type: application/json

{
  "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
  }  
} 

You should be able to access the variables in Lambda

params.Item = event.query; to access the query parameters

Check out these links for more information http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

https://kennbrodhagen.net/2015/12/06/how-to-create-a-request-object-for-your-lambda-event-from-api-gateway/

Let me know if that helps.