Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail to enable CORS for API Gateway functions

I get

enter image description here

While attempting to enable CORS on API Gateway, why is that and how do I resolve this? These functions are deployed using AWS SAM. But I notice if I create my own APIs via AWS Console this happens too

The errors looks like:

  • invalid model name specified application/json=Empty
  • invalid response status code specified

I found I seem to need to add an "Empty" response model myself?


Now, I get

Add Access-Control-Allow-Origin Integration Response Header Mapping to POST method (invalid response status code specified)

How do I resolve this?

like image 645
Jiew Meng Avatar asked Aug 22 '17 14:08

Jiew Meng


People also ask

How do I fix the CORS issue in API gateway?

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.

What is CORS in API gateway?

Cross-origin resource sharing (CORS) is a browser security feature that restricts HTTP requests that are initiated from scripts running in the browser. CORS is typically required to build web applications that access APIs hosted on a different domain or origin.


2 Answers

Firstly please select your root resource and select "Enable CORS". It will enable CORS to all methods. Ideally it should work. If in case it doesn't work Please add an empty json in the response as I have marked in the screenshot attached. I believe you don't have any default response header added in your OPTIONS method response (in Method Response ). Please refer screenshot

enter image description here

like image 54
Vijayanath Viswanathan Avatar answered Sep 28 '22 15:09

Vijayanath Viswanathan


Create a new model from the left menu that you will call Empty and it works

empty model

like image 42
MrLuje Avatar answered Sep 28 '22 17:09

MrLuje