I am trying to figure out how to map a Response from Lambda in the API Gateway to different status codes and at the same time receive a JSON object from my Lambda-function.
I have the following in Lambda:
context.done('Not Found:',jsonObject);
And in my API Gateway, in integration response I have a Lambda error regex on 403 saying Not Found:.*. This works, the method is returning a returning a 403.
The problem is I can't seem to return the jsonObject. I have tried to create a application/json mapping template that looks like this (also under integration response):
{"error" : $input.json('$')}
But that only results in my response looking like this:
{"error" : {"errorMessage":"Not Found:"}}
Am I misunderstanding the mapping template?
Navigate to the Integration Response for the API's GET method. Open up the 200 Response, Header Mappings, and Mapping Templates. Edit the Response Header Content-Type and set the Mapping value to 'text/html' (be sure to use single quotes). Don't forget to save it with the green checkmark icon.
If you want to stick with Lambda's default binding behavior, this approach looks promising.
Is there a way to change the http status codes returned by Amazon API Gateway?
Also Lambda will ignore the second parameter if the first error
parameter is non-null.
Here's some cases how Lambda works.
exports.handler = function(event, context) {
context.done(null, {hello: 'world'});
}
Result: Lambda only returns the second parameter in JSON object.
{"hello": "world"}
exports.handler = function(event, context) {
context.done({ping: 'pong'}, {hello: 'world'});
}
Result: Lambda automatically binds the first parameter to errorMessage
value. Notice the second parameter {hello: 'world'}
is dropped off.Better not to pass object as it results in [object Object]
.
{"errorMessage": "[object Object]"}
exports.handler = function(event, context) {
context.done('pingpong', {hello: 'world'});
}
Result: Lambda automatically binds the first parameter to errorMessage
value. Notice the second parameter {hello: 'world'}
is dropped off.
{"errorMessage": "pingpong"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With