3.7 KiB
Custom Response Classes - File, HTML, Redirect, Streaming, etc.
There are several custom response classes you can use to create an instance and return them directly from your path operations.
Read more about it in the FastAPI docs for Custom Response - HTML, Stream, File, others.
You can import them directly from fastapi.responses
:
from fastapi.responses import (
FileResponse,
HTMLResponse,
JSONResponse,
ORJSONResponse,
PlainTextResponse,
RedirectResponse,
Response,
StreamingResponse,
UJSONResponse,
)
FastAPI Responses
There are a couple of custom FastAPI response classes, you can use them to optimize JSON performance.
::: fastapi.responses.UJSONResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
::: fastapi.responses.ORJSONResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
Starlette Responses
::: fastapi.responses.FileResponse options: members: - chunk_size - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
::: fastapi.responses.HTMLResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
::: fastapi.responses.JSONResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
::: fastapi.responses.PlainTextResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
::: fastapi.responses.RedirectResponse options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
::: fastapi.responses.Response options: members: - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie
::: fastapi.responses.StreamingResponse options: members: - body_iterator - charset - status_code - media_type - body - background - raw_headers - render - init_headers - headers - set_cookie - delete_cookie