fastapi/docs/em/docs/tutorial/cors.md

4.0 KiB
Raw Blame History

⚜ (✖️-🇨🇳 🤝)

⚖️ "✖️-🇨🇳 🤝" 🔗🕐 🕸 🏃‍♂ 🖥 ✔️ 🕸 📟 👈 🔗 ⏮️ 👩‍💻, & 👩‍💻 🎏 "🇨🇳" 🌘 🕸.

🇨🇳

🇨🇳 🌀 🛠️ (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) ⚖️ 🌐 👫 ⏮️ 🃏 "*".
  • 🎯 🇺🇸🔍 🎚 ⚖️ 🌐 👫 ⏮️ 🃏 "*".

{* ../../docs_src/cors/tutorial001.py hl[2,6:11,13:19] *}

🔢 🔢 ⚙️ 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 🎚. 👉 💼 🛠️ 🔜 🚶‍♀️ 📨 🔘 😐, 🔜 🔌 ☑ ⚜ 🎚 🔛 📨.

🌅

🌖 🔃 , 🦎🧾.

/// note | 📡

👆 💪 ⚙️ from starlette.middleware.cors import CORSMiddleware.

FastAPI 🚚 📚 🛠️ fastapi.middleware 🏪 👆, 👩‍💻. 🌅 💪 🛠️ 👟 🔗 ➡️ 💃.

///