CORS(Cross-Origin Resource Sharing)は、あるオリジンからのウェブページが別のオリジンのAPIを呼び出せるかどうかを制御するブラウザのセキュリティメカニズムです。FastAPIは組み込みの**CORSMiddleware**で設定します。異なるドメイン/ポートのフロントエンドがAPIを呼び出すたびにCORSに遭遇します。
CORSが対処する問題
A React app at http://localhost:3000 calling an API at http://localhost:8000 is
CROSS-ORIGIN (different port). The BROWSER blocks the response unless the API
sends CORS headers permitting that origin.
(origin = scheme + host + port)
CORSMiddlewareの設定
fastapi.middleware.cors CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=[, ],
allow_credentials=,
allow_methods=[],
allow_headers=[],
)
