Home [FastAPI] CORS
Post
Cancel

[FastAPI] CORS

1
Access to fetch at 'http://localhost:8000/' from origin 'http://localhost:19006' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

FE에서 http://localhost:8000 호출시 위 에러 발생 요청하는 호스트 주소를 origins에 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from typing import Optional

from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()
origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

https://fastapi.tiangolo.com/tutorial/cors/#origin

This post is licensed under CC BY 4.0 by the author.

[Python] 쿠팡 이미지, 상품명, 가격 스크랩

FastAPI 시작