Skip to content

🌖 👔 📟

🔢, FastAPI 🔜 📨 📨 ⚙️ JSONResponse, 🚮 🎚 👆 📨 ⚪️➡️ 👆 ➡ 🛠️ 🔘 👈 JSONResponse.

⚫️ 🔜 ⚙️ 🔢 👔 📟 ⚖️ 1️⃣ 👆 ⚒ 👆 ➡ 🛠️.

🌖 👔 📟

🚥 👆 💚 📨 🌖 👔 📟 ↖️ ⚪️➡️ 👑 1️⃣, 👆 💪 👈 🛬 Response 🔗, 💖 JSONResponse, & ⚒ 🌖 👔 📟 🔗.

🖼, ➡️ 💬 👈 👆 💚 ✔️ ➡ 🛠️ 👈 ✔ ℹ 🏬, & 📨 🇺🇸🔍 👔 📟 2️⃣0️⃣0️⃣ "👌" 🕐❔ 🏆.

✋️ 👆 💚 ⚫️ 🚫 🆕 🏬. & 🕐❔ 🏬 🚫 🔀 ⏭, ⚫️ ✍ 👫, & 📨 🇺🇸🔍 👔 📟 2️⃣0️⃣1️⃣ "✍".

🏆 👈, 🗄 JSONResponse, & 📨 👆 🎚 📤 🔗, ⚒ status_code 👈 👆 💚:

from typing import Union

from fastapi import Body, FastAPI, status
from fastapi.responses import JSONResponse

app = FastAPI()

items = {"foo": {"name": "Fighters", "size": 6}, "bar": {"name": "Tenders", "size": 3}}


@app.put("/items/{item_id}")
async def upsert_item(
    item_id: str,
    name: Union[str, None] = Body(default=None),
    size: Union[int, None] = Body(default=None),
):
    if item_id in items:
        item = items[item_id]
        item["name"] = name
        item["size"] = size
        return item
    else:
        item = {"name": name, "size": size}
        items[item_id] = item
        return JSONResponse(status_code=status.HTTP_201_CREATED, content=item)

Warning

🕐❔ 👆 📨 Response 🔗, 💖 🖼 🔛, ⚫️ 🔜 📨 🔗.

⚫️ 🏆 🚫 🎻 ⏮️ 🏷, ♒️.

⚒ 💭 ⚫️ ✔️ 📊 👆 💚 ⚫️ ✔️, & 👈 💲 ☑ 🎻 (🚥 👆 ⚙️ JSONResponse).

📡 ℹ

👆 💪 ⚙️ from starlette.responses import JSONResponse.

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

🗄 & 🛠️ 🩺

🚥 👆 📨 🌖 👔 📟 & 📨 🔗, 👫 🏆 🚫 🔌 🗄 🔗 (🛠️ 🩺), ↩️ FastAPI 🚫 ✔️ 🌌 💭 ⏪ ⚫️❔ 👆 🚶 📨.

✋️ 👆 💪 📄 👈 👆 📟, ⚙️: 🌖 📨.