You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

9.8 KiB

🛃 📨 - 🕸, 🎏, 📁, 🎏

🔢, FastAPI 🔜 📨 📨 ⚙️ JSONResponse.

👆 💪 🔐 🛬 Response 🔗 👀 📨 📨 🔗{.internal-link target=_blank}.

🚥 👆 📨 Response 🔗, 📊 🏆 🚫 🔁 🗜, & 🧾 🏆 🚫 🔁 🏗 (🖼, 🔌 🎯 "📻 🆎", 🇺🇸🔍 🎚 Content-Type 🍕 🏗 🗄).

👆 💪 📣 Response 👈 👆 💚 ⚙️, 🛠️ 👨‍🎨.

🎚 👈 👆 📨 ➡️ 👆 🛠️ 🔢 🔜 🚮 🔘 👈 Response.

& 🚥 👈 Response ✔️ 🎻 📻 🆎 (application/json), 💖 💼 ⏮️ JSONResponse & UJSONResponse, 💽 👆 📨 🔜 🔁 🗜 (& ) ⏮️ 🙆 Pydantic response_model 👈 👆 📣 🛠️ 👨‍🎨.

!!! note 🚥 👆 ⚙️ 📨 🎓 ⏮️ 🙅‍♂ 📻 🆎, FastAPI 🔜 👆 📨 ✔️ 🙅‍♂ 🎚, 🔜 🚫 📄 📨 📁 🚮 🏗 🗄 🩺.

⚙️ ORJSONResponse

🖼, 🚥 👆 🎭, 👆 💪 & ⚙️ orjson & ⚒ 📨 ORJSONResponse.

🗄 Response 🎓 (🎧-🎓) 👆 💚 ⚙️ & 📣 🛠️ 👨‍🎨.

📨, 📨 Response 🔗 🌅 🌘 🛬 📖.

👉 ↩️ 🔢, FastAPI 🔜🔠 🏬 🔘 & ⚒ 💭 🎻 ⏮️ 🎻, ⚙️ 🎏 🎻 🔗 🔢{.internal-link target=_blank} 🔬 🔰. 👉 👆 📨 🎚, 🖼 💽 🏷.

🚥 👆 🎯 👈 🎚 👈 👆 🛬 🎻 ⏮️ 🎻, 👆 💪 🚶‍♀️ 🔗 📨 🎓 & 🌥 👈 FastAPI 🔜 ✔️ 🚶‍♀️ 👆 📨 🎚 🔘 jsonable_encoder🚶‍♀️ 📨 🎓.

{!../../../docs_src/custom_response/tutorial001b.py!}

!!! info 🔢 response_class 🔜 ⚙️ 🔬 "📻 🆎" 📨.

👉 💼, 🇺🇸🔍 🎚 `Content-Type` 🔜 ⚒ `application/json`.

 & ⚫️ 🔜 📄 ✅ 🗄.

!!! tip ORJSONResponse 🕴 💪 FastAPI, 🚫 💃.

🕸 📨

📨 📨 ⏮️ 🕸 🔗 ➡️ FastAPI, ⚙️ HTMLResponse.

  • 🗄 HTMLResponse.
  • 🚶‍♀️ HTMLResponse 🔢 response_class 👆 🛠️ 👨‍🎨.
{!../../../docs_src/custom_response/tutorial002.py!}

!!! info 🔢 response_class 🔜 ⚙️ 🔬 "📻 🆎" 📨.

👉 💼, 🇺🇸🔍 🎚 `Content-Type` 🔜 ⚒ `text/html`.

 & ⚫️ 🔜 📄 ✅ 🗄.

📨 Response

👀 📨 📨 🔗{.internal-link target=_blank}, 👆 💪 🔐 📨 🔗 👆 🛠️, 🛬 ️.

🎏 🖼 ➡️ 🔛, 🛬 HTMLResponse, 💪 👀 💖:

{!../../../docs_src/custom_response/tutorial003.py!}

!!! warning Response 📨 🔗 👆 🛠️ 🔢 🏆 🚫 📄 🗄 (🖼, Content-Type 🏆 🚫 📄) & 🏆 🚫 🏧 🎓 🩺.

!!! info ↗️, ☑ Content-Type 🎚, 👔 📟, ️, 🔜 👟 ➡️ Response 🎚 👆 📨.

📄 🗄 & 🔐 Response

🚥 👆 💚 🔐 📨 ➡️ 🔘 🔢 🎏 🕰 📄 "📻 🆎" 🗄, 👆 💪 ⚙️ response_class 🔢 & 📨 Response 🎚.

response_class 🔜 ⤴️ ⚙️ 🕴 📄 🗄 🛠️, 👆 Response 🔜 ⚙️.

📨 HTMLResponse 🔗

🖼, 💪 🕳 💖:

{!../../../docs_src/custom_response/tutorial004.py!}

👉 🖼, 🔢 generate_html_response() 🏗 & 📨 Response ↩️ 🛬 🕸 str.

🛬 🏁 🤙 generate_html_response(), 👆 🛬 Response 👈 🔜 🔐 🔢 FastAPI 🎭.

👆 🚶‍♀️ HTMLResponse response_class 💁‍♂️, FastAPI 🔜 💭 📄 ️ 🗄 & 🎓 🩺 🕸 ⏮️ text/html:

💪 📨

📥 💪 📨.

✔️ 🤯 👈 👆 💪 ⚙️ Response 📨 🕳 🙆, ⚖️🛃 🎧-🎓.

!!! note "📡 ℹ" 👆 💪 ⚙️ from starlette.responses import HTMLResponse.

**FastAPI** 🚚 🎏 `starlette.responses` `fastapi.responses` 🏪 👆, 👩‍💻. ✋️ 🌅 💪 📨 👟 🔗 ⚪️➡️ 💃.

Response

👑 Response 🎓, 🌐 🎏 📨 😖 ➡️ ️.

👆 💪 📨 🔗.

🚫 📄 🔢:

  • content - str ⚖️ bytes.
  • status_code - int 🇺🇸🔍 👔 📟.
  • headers - dict 🎻.
  • media_type - str 🤝 📻 🆎. 🤶 Ⓜ. "text/html".

FastAPI (🤙 💃) 🔜 🔁 🔌 🎚-📐 🎚. 🔜 🔌 🎚-🆎 🎚, 🔛 = & 🔁 = ✍ 🆎.

{!../../../docs_src/response_directly/tutorial002.py!}

HTMLResponse

⚖️ 🔢 & 📨 🕸 📨, 👆🔛.

PlainTextResponse

⚖️ 🔢 & 📨 📨.

{!../../../docs_src/custom_response/tutorial005.py!}

JSONResponse

💽 & 📨 application/json 🗜 📨.

👉 🔢 📨 ⚙️ FastAPI, 👆🔛.

ORJSONResponse

🎛 🎻 📨 ⚙️ orjson, 👆🔛.

UJSONResponse

🎛 🎻 📨 ⚙️ ujson.

!!! warning ujson 🌘 💛 🌘 🐍 🏗-🛠️ 🍵 📐-💼.

{!../../../docs_src/custom_response/tutorial001.py!}

!!! tip 💪 👈 ORJSONResponse 💪 🎛.

RedirectResponse

📨 🇺🇸🔍 . ⚙️ 3️⃣0️⃣7️⃣ 👔 📟 (🍕 ) 🔢.

👆 💪 📨 RedirectResponse 🔗:

{!../../../docs_src/custom_response/tutorial006.py!}

⚖️ 👆 💪 ⚙️ response_class 🔢:

{!../../../docs_src/custom_response/tutorial006b.py!}

🚥 👆 👈, ⤴️ 👆 💪 📨 📛 🔗 ➡️ 👆 🛠️ 🔢.

👉 💼, status_code ⚙️ 🔜 🔢 1️⃣ RedirectResponse, 307.


👆 💪 ⚙️ status_code 🔢 🌀 ⏮️ response_class 🔢:

{!../../../docs_src/custom_response/tutorial006c.py!}

StreamingResponse

🔁 🚂 ⚖️ 😐 🚂/🎻 & 🎏 📨 💪.

{!../../../docs_src/custom_response/tutorial007.py!}

⚙️ StreamingResponse ⏮️ 📁-💖 🎚

🚥 👆 ✔️ 📁-💖 🎚 ( 🎚 📨 open()), 👆 💪🚂 🔢 🔁 🤭 👈 📁-💖 🎚.

👈 🌌, 👆 🚫 ✔️🌐 🥇 💾, & 👆 💪 🚶‍♀️ 👈 🚂 🔢 StreamingResponse, & 📨 ️.

👉 🔌 📚 🗃 🔗 ⏮️💾, 📹 🏭, & 🎏.

{!../../../docs_src/custom_response/tutorial008.py!}

1️⃣. 👉 🚂 🔢. ️ "🚂 🔢" ↩️ 🔌 yield 📄 🔘. 2️⃣. ⚙️ with 🍫, 👥💭 👈 📁-💖 🎚 📪 ⏮️ 🚂 🔢 🔨. , ⏮️ 🏁 📨 📨. 3️⃣. 👉 yield from 💬 🔢 🔁 🤭 👈 👜 🌟 file_like. & ⤴️, 🔠 🍕 🔁, 🌾 👈 🍕 👟 ➡️ 👉 🚂 🔢.

, ⚫️ 🚂 🔢 👈 📨 "🏭" 👷 🕳 🙆 🔘.

🔨 ⚫️ 👉 🌌, 👥 💪 🚮 ⚫️ `with` 🍫, & 👈 🌌, 🚚 👈 ⚫️ 📪 ⏮️ 🏁.

!!! tip 👀 👈 📥 👥 ⚙️ 🐩 open() 👈 🚫 🐕‍🦺 async & await, 👥 📣🛠️ ⏮️ 😐 def.

FileResponse

🔁 🎏 📁 📨.

🎏 🔗 🌘 🎏 📨 🆎:

  • path - 📁 📁 🎏.
  • headers - 🙆 🛃 🎚 🔌, 📖.
  • media_type - 🎻 🤝 📻 🆎. 🚥 🔢, 📁 ⚖️🔜 ⚙️ 🔑 📻 🆎.
  • filename - 🚥 ⚒, 👉 🔜 🔌 📨 Content-Disposition.

📁 📨 🔜 🔌Content-Length, Last-Modified & ETag 🎚.

{!../../../docs_src/custom_response/tutorial009.py!}

👆 💪 ⚙️ response_class 🔢:

{!../../../docs_src/custom_response/tutorial009b.py!}

👉 💼, 👆 💪 📨 📁🔗 ➡️ 👆 🛠️ 🔢.

🛃 📨 🎓

👆 💪👆 👍 🛃 📨 🎓, 😖 ➡️ Response & ⚙️ ️.

🖼, ➡️ 💬 👈 👆 💚 ⚙️ orjson, ⏮️ 🛃🚫 ⚙️ 🔌 ORJSONResponse 🎓.

➡️ 💬 👆 💚 📨 🔂 & 📁 🎻, 👆 💚 ⚙️ Orjson 🎛 orjson.OPT_INDENT_2.

👆 💪CustomORJSONResponse. 👑 👜 👆 ✔️Response.render(content) 👩‍🔬 👈 📨 🎚 bytes:

{!../../../docs_src/custom_response/tutorial009c.py!}

🔜 ↩️ 🛬:

{"message": "Hello World"}

...👉 📨 🔜 📨:

{
  "message": "Hello World"
}

↗️, 👆 🔜 🎲 🔎 🌅 👍 🌌 📈 👉 🌘 🎻. 👶

🔢 📨 🎓

🕐 🏗 FastAPI 🎓 👐 ⚖️ APIRouter 👆 💪 📨 🎓 ⚙️ 🔢.

🔢 👈 🔬 👉 default_response_class.

🖼 🔛, FastAPI 🔜 ⚙️ ORJSONResponse 🔢, 🌐 🛠️, ↩️ JSONResponse.

{!../../../docs_src/custom_response/tutorial010.py!}

!!! tip 👆 💪 🔐 response_class 🛠️ ⏭.

🌖 🧾

👆 💪 📣 📻 🆎 & 📚 🎏 ℹ 🗄 ⚙️ responses: 🌖 📨 🗄{.internal-link target=_blank}.