You would need to enable CORS (Cross-Origin Resource Sharing) in the FastAPI backend. You can configure it in your FastAPI application using the CORSMiddleware
.
Note
Origin
An origin is the combination of protocol (
http
,https
), domain
(myapp.com
,localhost
,localhost.tiangolo.com
), and port (80
,
443
,8080
).So, all these are different origins:
http://localhost
https://localhost
http://localhost:8080
Even if they are all in
localhost
, they use different protocols or
ports, so, they are different “origins”.
Example
Have a look at this related answer as well.
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = ["http://localhost:3000", "http://127.0.0.1:3000"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)