1

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.

2
  • 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? Commented Dec 12, 2016 at 1:55
  • I'd like to know how to do both Commented Dec 12, 2016 at 2:08

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

That's right. I see the response body when I test it. However, if I call it from outside, I get the following: {"message":"Forbidden"}
You got it working? Where do you see message: forbidden in the Lambda function or in the response in the browser?
I see it in the response in the browser
Nevermind, I got it to work. I had to deploy the API. Now, I can see the response. Woohooo!!! Thanks a lot.
Great! Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.