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://pydantic-docs.helpmanual.io/"

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: ๐ŸŒ– ๐Ÿ“จ ๐Ÿ—„.