I'm trying to enable CORS in this very basic FastAPI example, however it doesn't seem to be working.
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['*']
)
@app.get('/')
def read_main():
return {'message': 'Hello World!'}
This is the response I get:
curl -v http://127.0.0.1:8000
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< date: Fri, 08 Jan 2021 19:27:37 GMT
< server: uvicorn
< content-length: 26
< content-type: application/json
<
* Connection #0 to host 127.0.0.1 left intact
{"message":"Hello World!"}*
Origin
in the headers because it has no well defined origin, so it cannot return it in the headers – ContaminationVersion 87.0.4280.88 (Official Build) snap (64-bit)
) – ContaminationCORS
permissions are mandatory yes, but in the case of an API requests from different sources/domains will be performed, then no.CORS
are mainly for security reasons (scripts that perform requests to external resources) – Contamination