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?
    – Yan
    Dec 12, 2016 at 1:55
  • I'd like to know how to do both
    – froinds
    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.

5
  • 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"}
    – froinds
    Dec 12, 2016 at 2:27
  • You got it working? Where do you see message: forbidden in the Lambda function or in the response in the browser?
    – Yan
    Dec 12, 2016 at 2:53
  • I see it in the response in the browser
    – froinds
    Dec 12, 2016 at 2:57
  • 1
    Nevermind, I got it to work. I had to deploy the API. Now, I can see the response. Woohooo!!! Thanks a lot.
    – froinds
    Dec 12, 2016 at 3:02
  • Great! Happy coding!
    – Yan
    Dec 12, 2016 at 3:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.