Skip to content
Advertisement

Incorrect Access-Control-Allow-Origin being added automatically to POST & DELETE endpoints [closed]

I have an app built with FastAPI hosted on API Gateway using serverless.

The API: https://xxxxx.execute-api.xx-xxxxxx-x.amazonaws.com/dev/{proxy+}

Since most of my endpoints are proxy endpoints, I am adding to the response headers as follows:

JavaScript

I have 3 different types of endpoints: GET, POST & DELETE.

The Access-Control-Allow-Origin is correctly assigned in the GET request as follows:

JavaScript

it is not correctly being assigned in the POST & DELETE methods. When I host it on API Gateway, the above-mentioned API is being automatically added to the Access-Control-Allow-Origin in place of "*", which I am specifically mentioning when I declare the response headers as shown above.

The response headers for the POST & DELETE methods:

JavaScript

Because of this, the following error is shown in the frontend:

Failed to load resource: Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

Is there something that I am missing that should be added for these methods?

Thanks

Edit 1:

I’m setting the response headers to all the endpoints as follows:

JavaScript

I am also following the structure here. So in my main.py, as done here, I have

JavaScript

Edit 2

When I inspect the response, I see that there are 2 APIs:

enter image description here

This is for the POST (which doesn’t have the response headers I mention) enter image description here

This is for the OPTIONS method enter image description here

Advertisement

Answer

After spending hours on this issue and coordinating with the frontend developer, we realized that there was a missing parameter in the frontend that was required for the backend. It was causing an internal server error, that’s why the headers were not available.

After adding the parameter, the requests were successful.

Advertisement