Skip to content

🛃 📨 - 🕸, 🎏, 📁, 🎏

🔢, FastAPI 🔜 📨 📨 ⚙️ JSONResponse.

👆 💪 🔐 ⚫️ 🛬 Response 🔗 👀 📨 📨 🔗.

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

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

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

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

Note

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

⚙️ ORJSONResponse

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

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

⭕ 📨, 📨 Response 🔗 🌅 ⏩ 🌘 🛬 📖.

👉 ↩️ 🔢, FastAPI 🔜 ✔ 🔠 🏬 🔘 & ⚒ 💭 ⚫️ 🎻 ⏮️ 🎻, ⚙️ 🎏 🎻 🔗 🔢 🔬 🔰. 👉 ⚫️❔ ✔ 👆 📨 ❌ 🎚, 🖼 💽 🏷.

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

from fastapi import FastAPI
from fastapi.responses import ORJSONResponse

app = FastAPI()


@app.get("/items/", response_class=ORJSONResponse)
async def read_items():
    return ORJSONResponse([{"item_id": "Foo"}])

Info

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

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

& ⚫️ 🔜 📄 ✅ 🗄.

Tip

ORJSONResponse ⏳ 🕴 💪 FastAPI, 🚫 💃.

🕸 📨

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

  • 🗄 HTMLResponse.
  • 🚶‍♀️ HTMLResponse 🔢 response_class 👆 ➡ 🛠️ 👨‍🎨.
from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return """
    <html>
        <head>
            <title>Some HTML in here</title>
        </head>
        <body>
            <h1>Look ma! HTML!</h1>
        </body>
    </html>
    """

Info

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

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

& ⚫️ 🔜 📄 ✅ 🗄.

📨 Response

👀 📨 📨 🔗, 👆 💪 🔐 📨 🔗 👆 ➡ 🛠️, 🛬 ⚫️.

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

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/items/")
async def read_items():
    html_content = """
    <html>
        <head>
            <title>Some HTML in here</title>
        </head>
        <body>
            <h1>Look ma! HTML!</h1>
        </body>
    </html>
    """
    return HTMLResponse(content=html_content, status_code=200)

Warning

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

Info

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

📄 🗄 & 🔐 Response

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

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

📨 HTMLResponse 🔗

🖼, ⚫️ 💪 🕳 💖:

from fastapi import FastAPI
from fastapi.responses import HTMLResponse

app = FastAPI()


def generate_html_response():
    html_content = """
    <html>
        <head>
            <title>Some HTML in here</title>
        </head>
        <body>
            <h1>Look ma! HTML!</h1>
        </body>
    </html>
    """
    return HTMLResponse(content=html_content, status_code=200)


@app.get("/items/", response_class=HTMLResponse)
async def read_items():
    return generate_html_response()

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

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

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

💪 📨

📥 💪 📨.

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

📡 ℹ

👆 💪 ⚙️ 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 (🤙 💃) 🔜 🔁 🔌 🎚-📐 🎚. ⚫️ 🔜 🔌 🎚-🆎 🎚, ⚓️ 🔛 = & 🔁 = ✍ 🆎.

from fastapi import FastAPI, Response

app = FastAPI()


@app.get("/legacy/")
def get_legacy_data():
    data = """<?xml version="1.0"?>
    <shampoo>
    <Header>
        Apply shampoo here.
    </Header>
    <Body>
        You'll have to use soap here.
    </Body>
    </shampoo>
    """
    return Response(content=data, media_type="application/xml")

HTMLResponse

✊ ✍ ⚖️ 🔢 & 📨 🕸 📨, 👆 ✍ 🔛.

PlainTextResponse

✊ ✍ ⚖️ 🔢 & 📨 ✅ ✍ 📨.

from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

app = FastAPI()


@app.get("/", response_class=PlainTextResponse)
async def main():
    return "Hello World"

JSONResponse

✊ 💽 & 📨 application/json 🗜 📨.

👉 🔢 📨 ⚙️ FastAPI, 👆 ✍ 🔛.

ORJSONResponse

⏩ 🎛 🎻 📨 ⚙️ orjson, 👆 ✍ 🔛.

UJSONResponse

🎛 🎻 📨 ⚙️ ujson.

Warning

ujson 🌘 💛 🌘 🐍 🏗-🛠️ ❔ ⚫️ 🍵 📐-💼.

from fastapi import FastAPI
from fastapi.responses import UJSONResponse

app = FastAPI()


@app.get("/items/", response_class=UJSONResponse)
async def read_items():
    return [{"item_id": "Foo"}]

Tip

⚫️ 💪 👈 ORJSONResponse 💪 ⏩ 🎛.

RedirectResponse

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

👆 💪 📨 RedirectResponse 🔗:

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/typer")
async def redirect_typer():
    return RedirectResponse("https://typer.tiangolo.com")

⚖️ 👆 💪 ⚙️ ⚫️ response_class 🔢:

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/fastapi", response_class=RedirectResponse)
async def redirect_fastapi():
    return "https://fastapi.tiangolo.com"

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

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


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

from fastapi import FastAPI
from fastapi.responses import RedirectResponse

app = FastAPI()


@app.get("/pydantic", response_class=RedirectResponse, status_code=302)
async def redirect_pydantic():
    return "https://docs.pydantic.dev/"

StreamingResponse

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

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()


async def fake_video_streamer():
    for i in range(10):
        yield b"some fake video bytes"


@app.get("/")
async def main():
    return StreamingResponse(fake_video_streamer())

⚙️ StreamingResponse ⏮️ 📁-💖 🎚

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

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

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

from fastapi import FastAPI
from fastapi.responses import StreamingResponse

some_file_path = "large-video-file.mp4"
app = FastAPI()


@app.get("/")
def main():
    def iterfile():  # (1)
        with open(some_file_path, mode="rb") as file_like:  # (2)
            yield from file_like  # (3)

    return StreamingResponse(iterfile(), media_type="video/mp4")

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

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

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

Tip

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

FileResponse

🔁 🎏 📁 📨.

✊ 🎏 ⚒ ❌ 🔗 🌘 🎏 📨 🆎:

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

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

from fastapi import FastAPI
from fastapi.responses import FileResponse

some_file_path = "large-video-file.mp4"
app = FastAPI()


@app.get("/")
async def main():
    return FileResponse(some_file_path)

👆 💪 ⚙️ response_class 🔢:

from fastapi import FastAPI
from fastapi.responses import FileResponse

some_file_path = "large-video-file.mp4"
app = FastAPI()


@app.get("/", response_class=FileResponse)
async def main():
    return some_file_path

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

🛃 📨 🎓

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

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

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

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

from typing import Any

import orjson
from fastapi import FastAPI, Response

app = FastAPI()


class CustomORJSONResponse(Response):
    media_type = "application/json"

    def render(self, content: Any) -> bytes:
        assert orjson is not None, "orjson must be installed"
        return orjson.dumps(content, option=orjson.OPT_INDENT_2)


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

🔜 ↩️ 🛬:

{"message": "Hello World"}

...👉 📨 🔜 📨:

{
  "message": "Hello World"
}

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

🔢 📨 🎓

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

🔢 👈 🔬 👉 default_response_class.

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

from fastapi import FastAPI
from fastapi.responses import ORJSONResponse

app = FastAPI(default_response_class=ORJSONResponse)


@app.get("/items/")
async def read_items():
    return [{"item_id": "Foo"}]

Tip

👆 💪 🔐 response_class ➡ 🛠️ ⏭.

🌖 🧾

👆 💪 📣 📻 🆎 & 📚 🎏 ℹ 🗄 ⚙️ responses: 🌖 📨 🗄.