I have a response method that looks like this for my Lambda functions:
def respond(err, res=None):
return {
'statusCode': 400 if err else 200,
'body': json.dumps(err) if err else json.dumps(res),
'headers': {
'Access-Control-Allow-Headers': 'content-type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token',
'Access-Control-Allow-Methods': 'POST, GET, DELETE',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': True,
'Content-Type': 'application/json',
},
}
When I test my endpoint with an OPTIONS request from Postman, I get a 500 internal server error. If I test it from the the API Gateway console, I get this additionally:
Execution log for request test-request
Wed Jul 05 14:25:26 UTC 2017 : Starting execution for request: test-invoke-request
Wed Jul 05 14:25:26 UTC 2017 : HTTP Method: OPTIONS, Resource Path: /login
Wed Jul 05 14:25:26 UTC 2017 : Method request path: {}
Wed Jul 05 14:25:26 UTC 2017 : Method request query string: {}
Wed Jul 05 14:25:26 UTC 2017 : Method request headers: {}
Wed Jul 05 14:25:26 UTC 2017 : Method request body before transformations:
Wed Jul 05 14:25:26 UTC 2017 : Received response. Integration latency: 0 ms
Wed Jul 05 14:25:26 UTC 2017 : Endpoint response body before transformations:
Wed Jul 05 14:25:26 UTC 2017 : Endpoint response headers: {}
Wed Jul 05 14:25:26 UTC 2017 : Execution failed due to configuration error: Output mapping refers to an invalid method response: 200
Wed Jul 05 14:25:26 UTC 2017 : Method completed with status: 500
I'm not really sure what I'm doing wrong. I think I am returning all the right headers. Any help is appreciated.
Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.
To enable CORS for the Lambda proxy integration, you must add Access-Control-Allow-Origin: domain-name to the output headers . domain-name can be * for any domain name. The output body is marshalled to the frontend as the method response payload.
To support CORS, therefore, a REST API resource needs to implement an OPTIONS method that can respond to the OPTIONS preflight request with at least the following response headers mandated by the Fetch standard: Access-Control-Allow-Methods. Access-Control-Allow-Headers. Access-Control-Allow-Origin.
I think you are using lambda integration, but you seem to want the lambda proxy integration.
This error will be thrown if there is no default output configured on the integration response. Setup a method response and add a default mapping with the status code (or use lambda proxy integration).
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