Are you struggling with a frontend application that wants to use a backend AWS Lambda API ? Do you have the next CORS problem:
Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response
The solution is simple:
- implement HTTP OPTIONS method
- respond with the next access control headers : Access-Control-Allow-Origin, Access-Control-Allow-Headers, Access-Control-Allow-Methods
For example in python you can do something like this:
def return_200():
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,OPTIONS,POST,PUT',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
'Content-Type': 'application/json'
},
'body': json.dumps({'message': 'ok'})
}
Documentation and links:
- Request header field content-type - https://answers.netlify.com/t/request-header-field-content-type-is-not-allowed-by-access-control-allow-headers-in-preflight-response/54410