I'm trying to get a fastapi deployed (usually work in Node) and I'm getting a CORS error consistently. I've tried, wildcarding, I've tried explicit URL lists. No change no matter which I do.
The root serves Swagger docs which are deployed, and the test of all endpoints are working as espected.
API = FastAPI(
title="DocDB DS API",
version="1.0.0",
docs_url="/",
)
API.db = MongoDB()
API.s3 = S3()
API.add_middleware(
CORSMiddleware,
allow_origins=["https://examplevercelurl.app"],
allow_credentials=True,
allow_methods=["GET", "POST", "DELETE", "PUT"],
allow_headers=["*"],
)
@API.get("/version")
async def version():
return API.version
@API.post("/search")
async def search(query: str, page_number: int = 0, results_per_page: int = 100):
start = page_number * results_per_page
stop = start + results_per_page
search_results = API.db.search(query)[start:stop]
count = API.db.count({"$text": {"$search": query}})
n_pages = ceil(count / results_per_page)
return {"Pages": n_pages, "Count": count, "Response": list(search_results)}
The console produces this error:
Access to XMLHttpRequest at 'https://example.herokuapp.com/search?query=gun&page_number=0&results_per_page=20' from origin 'https://examplevercelurl.app' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
The client is hosted at https://examplevercelurl.app (cannot provide actual URL but I have made sure once again that the address listed in the origins list is correct)