docs_src/cors/tutorial001_py310.py
raw| 1 | from fastapi import FastAPI |
| 2 | from fastapi.middleware.cors import CORSMiddleware |
| 3 | |
| 4 | app = FastAPI() |
| 5 | |
| 6 | origins = [ |
| 7 | "http://localhost.tiangolo.com", |
| 8 | "https://localhost.tiangolo.com", |
| 9 | "http://localhost", |
| 10 | "http://localhost:8080", |
| 11 | ] |
| 12 | |
| 13 | app.add_middleware( |
| 14 | CORSMiddleware, |
| 15 | allow_origins=origins, |
| 16 | allow_credentials=True, |
| 17 | allow_methods=["*"], |
| 18 | allow_headers=["*"], |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | @app.get("/") |
| 23 | async def main(): |
| 24 | return {"message": "Hello World"} |