Enable CORS when using AWS API Gateway Request Validator
Asked Answered
F

1

7

I setup AWS API Gateway with CORS successfully, when a request is valid I have a 200 status code and also CORS headers, that's nice.

But when AWS API Gateway Request Validator detects an invalid input, I have the status code is 400 but CORS headers are not sent...

That's really unfortunate, because the client sees a CORS errors instead of a real 400 error (for exemple with fetch the client is not able to distinguish 400 errors because of CORS errors).

This question may be related to Get detailed error messages from AWS API Gateway Request Validator

Frediafredie answered 9/2, 2018 at 16:56 Comment(0)
S
9

I came to this answer through a discussion about using serverless to deploy CORS services and dealing with errors thrown from API Gateway: https://github.com/serverless/serverless/issues/3896#issuecomment-333910525

Basically, you need to go to API Gateway, select your API, then select Gateway Responses. You can then add headers for any 4XX or 5XX response.

API Gateway, Gateway Responses

This will allow 400 errors thrown from API Gateway to work with CORS, since you're adding the needed headers here.

Sudra answered 13/2, 2018 at 4:15 Comment(1)
Beautiful, this is exactly what I was looking for. Just a note to others, this took a few minutes to take effect. You can test that it is responding as you expect by initially modifying the error JSON response with a custom message as well.Estus

© 2022 - 2024 — McMap. All rights reserved.