I have made an API in AWS API Gateway which calls a Lambda function. This is the code which I have used to return the application/json response almost similar to a Python Lambda Blueprint:
def response(status_code, response_body=None):
return {
'statusCode': status_code,
'body': json.dumps(response_body) if response_body else json.dumps({}),
'headers': {
'Content-Type': 'application/json',
},
}
Currently I have only one HTTP Status code 200. I am having a hard time making the model schema for this response.
How do I get the body out of this response and properly display it to the consumer?
EDIT: I needed to create my API with Lambda Proxy Integration because here I am returning the response from Lambda and not transforming it at all. Also, there is no need for Models Schemas here. For more info, read the accepted answer.
Note: To avoid No 'Access-Control-Allow-Origin' header is present on the requested resource. error. Simply add 'Access-Control-Allow-Origin': '*'
in the headers along with Content-Type
Cheers!
Just to make sure we're on the same page with API Gateway terminology:
Model schemas are only needed to model the input/output of your API if you plan to generate SDKs for your API (currently supports Java, iOS, Android, Javascript)
Mapping templates are what you can use to transform input from the method request into the integration request and output from the integration response into the final method response.
Proxy resource types in API Gateway allow you to proxy/pass-through the method request into your integration and integration response through to the method response without needing to deal with mapping templates, if you don't need to do any transformations.
Unless you explicitly need to transform your Lambda output at the API Gateway layer, I would recommend you look into the proxy resource type, which along with the ANY method and greedy path variables should simply your API Gateway configuration for the simplest pass-through/proxy use case.
https://aws.amazon.com/blogs/aws/api-gateway-update-new-features-simplify-api-development/
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