Skip to content

โšœ (โœ–๏ธ-๐Ÿ‡จ๐Ÿ‡ณ โ„น ๐Ÿค)

โšœ โš–๏ธ "โœ–๏ธ-๐Ÿ‡จ๐Ÿ‡ณ โ„น ๐Ÿค" ๐Ÿ”— โš  ๐Ÿ•โ” ๐Ÿ•ธ ๐Ÿƒโ€โ™‚ ๐Ÿ–ฅ โœ”๏ธ ๐Ÿ•ธ ๐Ÿ“Ÿ ๐Ÿ‘ˆ ๐Ÿ”— โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป, & ๐Ÿ‘ฉโ€๐Ÿ’ป ๐ŸŽ "๐Ÿ‡จ๐Ÿ‡ณ" ๐ŸŒ˜ ๐Ÿ•ธ.

๐Ÿ‡จ๐Ÿ‡ณ

๐Ÿ‡จ๐Ÿ‡ณ ๐ŸŒ€ ๐Ÿ› ๏ธ (http, https), ๐Ÿ†” (myapp.com, localhost, localhost.tiangolo.com), & โ›ด (80, 443, 8080).

, ๐ŸŒ ๐Ÿ‘ซ ๐ŸŽ ๐Ÿ‡จ๐Ÿ‡ณ:

  • http://localhost
  • https://localhost
  • http://localhost:8080

๐Ÿšฅ ๐Ÿ‘ซ ๐ŸŒ localhost, ๐Ÿ‘ซ โš™๏ธ ๐ŸŽ ๐Ÿ› ๏ธ โš–๏ธ โ›ด,, ๐Ÿ‘ซ ๐ŸŽ "๐Ÿ‡จ๐Ÿ‡ณ".

๐Ÿ”

, โžก๏ธ ๐Ÿ’ฌ ๐Ÿ‘† โœ”๏ธ ๐Ÿ•ธ ๐Ÿƒ ๐Ÿ‘† ๐Ÿ–ฅ http://localhost:8080, & ๐Ÿšฎ ๐Ÿ•ธ ๐Ÿ”„ ๐Ÿ”— โฎ๏ธ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿƒ http://localhost (โ†ฉ๏ธ ๐Ÿ‘ฅ ๐Ÿšซ โœ” โ›ด, ๐Ÿ–ฅ ๐Ÿ”œ ๐Ÿค” ๐Ÿ”ข โ›ด 80).

โคด๏ธ, ๐Ÿ–ฅ ๐Ÿ”œ ๐Ÿ“จ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” OPTIONS ๐Ÿ“จ ๐Ÿ‘ฉโ€๐Ÿ’ป, & ๐Ÿšฅ ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ“จ โ˜‘ ๐ŸŽš โœ” ๐Ÿ“ป โšช๏ธโžก๏ธ ๐Ÿ‘‰ ๐ŸŽ ๐Ÿ‡จ๐Ÿ‡ณ (http://localhost:8080) โคด๏ธ ๐Ÿ–ฅ ๐Ÿ”œ โžก๏ธ ๐Ÿ•ธ ๐Ÿ•ธ ๐Ÿ“จ ๐Ÿšฎ ๐Ÿ“จ ๐Ÿ‘ฉโ€๐Ÿ’ป.

๐Ÿ† ๐Ÿ‘‰, ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ”œ โœ”๏ธ ๐Ÿ“‡ "โœ” ๐Ÿ‡จ๐Ÿ‡ณ".

๐Ÿ‘‰ ๐Ÿ’ผ, โšซ๏ธ ๐Ÿ”œ โœ”๏ธ ๐Ÿ”Œ http://localhost:8080 ๐Ÿ•ธ ๐Ÿ‘ท โ˜‘.

๐Ÿƒ

โšซ๏ธ ๐Ÿ’ช ๐Ÿ“ฃ ๐Ÿ“‡ "*" ("๐Ÿƒ") ๐Ÿ’ฌ ๐Ÿ‘ˆ ๐ŸŒ โœ”.

โœ‹๏ธ ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ•ด โœ” ๐ŸŽฏ ๐Ÿ†Ž ๐Ÿ“ป, ๐Ÿšซ ๐ŸŒ ๐Ÿ‘ˆ ๐Ÿ”Œ ๐ŸŽ“: ๐Ÿช, โœ” ๐ŸŽš ๐Ÿ’– ๐Ÿ“š โš™๏ธ โฎ๏ธ ๐Ÿ“จ ๐Ÿค, โ™’๏ธ.

, ๐ŸŒ ๐Ÿ‘ท โ˜‘, โšซ๏ธ ๐Ÿ‘ป โœ” ๐ŸŽฏ โœ” ๐Ÿ‡จ๐Ÿ‡ณ.

โš™๏ธ CORSMiddleware

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”— โšซ๏ธ ๐Ÿ‘† FastAPI ๐Ÿˆธ โš™๏ธ CORSMiddleware.

  • ๐Ÿ—„ CORSMiddleware.
  • โœ ๐Ÿ“‡ โœ” ๐Ÿ‡จ๐Ÿ‡ณ (๐ŸŽป).
  • ๐Ÿšฎ โšซ๏ธ "๐Ÿ› ๏ธ" ๐Ÿ‘† FastAPI ๐Ÿˆธ.

๐Ÿ‘† ๐Ÿ’ช โœ” ๐Ÿšฅ ๐Ÿ‘† ๐Ÿ‘ฉโ€๐Ÿ’ป โœ”:

  • ๐ŸŽ“ (โœ” ๐ŸŽš, ๐Ÿช, โ™’๏ธ).
  • ๐ŸŽฏ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘ฉโ€๐Ÿ”ฌ (POST, PUT) โš–๏ธ ๐ŸŒ ๐Ÿ‘ซ โฎ๏ธ ๐Ÿƒ "*".
  • ๐ŸŽฏ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐ŸŽš โš–๏ธ ๐ŸŒ ๐Ÿ‘ซ โฎ๏ธ ๐Ÿƒ "*".
from fastapi import FastAPI
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=["*"],
)


@app.get("/")
async def main():
    return {"message": "Hello World"}

๐Ÿ”ข ๐Ÿ”ข โš™๏ธ CORSMiddleware ๐Ÿ› ๏ธ ๐Ÿšซ ๐Ÿ”ข, ๐Ÿ‘† ๐Ÿ”œ ๐Ÿ’ช ๐ŸŽฏ ๐Ÿ› ๏ธ ๐ŸŽฏ ๐Ÿ‡จ๐Ÿ‡ณ, ๐Ÿ‘ฉโ€๐Ÿ”ฌ, โš–๏ธ ๐ŸŽš, โœ” ๐Ÿ–ฅ โœ” โš™๏ธ ๐Ÿ‘ซ โœ–๏ธ-๐Ÿ†” ๐Ÿ”‘.

๐Ÿ“„ โŒ ๐Ÿ•โ€๐Ÿฆบ:

  • allow_origins - ๐Ÿ“‡ ๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ‘ˆ ๐Ÿ”œ โœ” โš’ โœ–๏ธ-๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ“จ. ๐Ÿคถ โ“‚. ['https://example.org', 'https://www.example.org']. ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ['*'] โœ” ๐Ÿ™† ๐Ÿ‡จ๐Ÿ‡ณ.
  • allow_origin_regex - ๐ŸŽป ๐ŸŽป ๐Ÿ ๐Ÿ›ก ๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ‘ˆ ๐Ÿ”œ โœ” โš’ โœ–๏ธ-๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ“จ. โœ… 'https://.*\.example\.org'.
  • allow_methods - ๐Ÿ“‡ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘ฉโ€๐Ÿ”ฌ ๐Ÿ‘ˆ ๐Ÿ”œ โœ” โœ–๏ธ-๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ“จ. ๐Ÿ”ข ['GET']. ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ['*'] โœ” ๐ŸŒ ๐Ÿฉ ๐Ÿ‘ฉโ€๐Ÿ”ฌ.
  • allow_headers - ๐Ÿ“‡ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ“จ ๐ŸŽš ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ•โ€๐Ÿฆบ โœ–๏ธ-๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ“จ. ๐Ÿ”ข []. ๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ['*'] โœ” ๐ŸŒ ๐ŸŽš. Accept, Accept-Language, Content-Language & Content-Type ๐ŸŽš ๐Ÿ•ง โœ” ๐Ÿ™… โšœ ๐Ÿ“จ.
  • allow_credentials - ๐ŸŽฆ ๐Ÿ‘ˆ ๐Ÿช ๐Ÿ”œ ๐Ÿ•โ€๐Ÿฆบ โœ–๏ธ-๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ“จ. ๐Ÿ”ข False. , allow_origins ๐Ÿšซ๐Ÿ”œ โš’ ['*'] ๐ŸŽ“ โœ”, ๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ”œ โœ”.
  • expose_headers - ๐ŸŽฆ ๐Ÿ™† ๐Ÿ“จ ๐ŸŽš ๐Ÿ‘ˆ ๐Ÿ”œ โš’ โ™ฟ ๐Ÿ–ฅ. ๐Ÿ”ข [].
  • max_age - โš’ ๐Ÿ”† ๐Ÿ•ฐ ๐Ÿฅˆ ๐Ÿ–ฅ ๐Ÿ’พ โšœ ๐Ÿ“จ. ๐Ÿ”ข 600.

๐Ÿ› ๏ธ ๐Ÿ“จ 2๏ธโƒฃ ๐ŸŽฏ ๐Ÿ†Ž ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ“จ...

โšœ ๐Ÿ›ซ ๐Ÿ“จ

๐Ÿ‘‰ ๐Ÿ™† OPTIONS ๐Ÿ“จ โฎ๏ธ Origin & Access-Control-Request-Method ๐ŸŽš.

๐Ÿ‘‰ ๐Ÿ’ผ ๐Ÿ› ๏ธ ๐Ÿ”œ ๐Ÿ†˜ ๐Ÿ“จ ๐Ÿ“จ & ๐Ÿ“จ โฎ๏ธ โ˜‘ โšœ ๐ŸŽš, & ๐Ÿ‘ฏโ€โ™‚๏ธ 200 โš–๏ธ 400 ๐Ÿ“จ ๐ŸŽ“ ๐ŸŽฏ.

๐Ÿ™… ๐Ÿ“จ

๐Ÿ™† ๐Ÿ“จ โฎ๏ธ Origin ๐ŸŽš. ๐Ÿ‘‰ ๐Ÿ’ผ ๐Ÿ› ๏ธ ๐Ÿ”œ ๐Ÿšถโ€โ™€๏ธ ๐Ÿ“จ ๐Ÿ”˜ ๐Ÿ˜, โœ‹๏ธ ๐Ÿ”œ ๐Ÿ”Œ โ˜‘ โšœ ๐ŸŽš ๐Ÿ”› ๐Ÿ“จ.

๐ŸŒ… โ„น

๐ŸŒ– โ„น ๐Ÿ”ƒ โšœ, โœ… ๐ŸฆŽ โšœ ๐Ÿงพ.

๐Ÿ“ก โ„น

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ from starlette.middleware.cors import CORSMiddleware.

FastAPI ๐Ÿšš ๐Ÿ“š ๐Ÿ› ๏ธ fastapi.middleware ๐Ÿช ๐Ÿ‘†, ๐Ÿ‘ฉโ€๐Ÿ’ป. โœ‹๏ธ ๐ŸŒ… ๐Ÿ’ช ๐Ÿ› ๏ธ ๐Ÿ‘Ÿ ๐Ÿ”— โšช๏ธโžก๏ธ ๐Ÿ’ƒ.