[BUG] I can't enable CORS
See original GitHub issueI set up a Dockerimage that uses fastapi version 0.45.0 and uvicorn. I need to enable CORS for different domains like id-localtest.mydomain.com
, id-alpha.mydomain.com
and id.mydomain.com
. In my main.py
file I wrote these lines of code
from fastapi import FastAPI, HTTPException
app = FastAPI()
origins = ['https://id-localtest.mydomain.com', 'https://id-alpha.mydomain.com', 'https://id.mydomain.com']
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["DELETE,GET,POST,PUT"],
allow_headers=["*"])
@app.post("/{stage}/activation")
async def activation_post(stage: str, request: Request):
# my custom code
return 200
I’m always getting an HTTP status code 400 when a generic browser makes an OPTIONS request.
If I try to use Postman to make an OPTIONS request, I get the HTTP status code 405
and the json received is:
{
"detail": "Method Not Allowed"
}
What am I doing wrong? I’m following this tutorial https://fastapi.tiangolo.com/tutorial/cors/
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
3 Ways to Fix the CORS Error — and How the Access-Control ...
Fix one: install the Allow-Control-Allow-Origin plugin. The quickest fix you can make is to install the moesif CORS extension .
Read more >Cant enable CORS on the server side NodeJS - Stack Overflow
You explicitly disallowed CORS on the client side by setting mode: 'same-origin' instead of the default mode: 'cors' . To quote the docs:....
Read more >What Is a CORS Error and How to Fix It (3 Ways) - Bannerbear
Solution 1: Configure the Backend to Allow CORS · Solution 2: Use a Proxy Server · Solution 3: Bypass the Error Using a...
Read more >Fixing Common Problems with CORS and JavaScript
Tutorial: This post walks through troubleshooting and fixing common problems associated with calling REST APIs from JavaScript.
Read more >Troubleshoot CORS errors from API Gateway - Amazon AWS
To resolve a CORS error from an API Gateway REST API or HTTP API, ... Configure CORS on your API resource that's experiencing...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
starlette.middleware.cors.CORSMiddleware
accepts a sequence of strings inallow_methods
parameter. Try replacing your line with:Also, your browser does not send a simple
OPTIONS
request, but a preflight-request. You can send it manually by specifying special headers for your request. Here is an example for your application usinghttpie
:please note that you can also add
in origins sections, for example:
you can also allow all standard methods by set
allow_methods=["*"]
for example: