Importing
Response Classes
Response
Response
Response
The base response class. Use this for custom responses or when you need full control.Constructor Parameters:bytes | str
default:"b''"
The response body content.
int
default:"200"
HTTP status code.
dict[str, str] | None
default:"None"
HTTP headers.
str | None
default:"None"
Media type (Content-Type).
BackgroundTask | None
default:"None"
Background task to run after sending the response.
int
The HTTP status code.
MutableHeaders
Response headers.
bytes
The response body.
str | None
The media type (Content-Type).
BackgroundTask | None
Background task to execute after response.
set_cookie()- Set a cookiedelete_cookie()- Delete a cookie
JSONResponse
JSONResponse
JSONResponse
Returns a JSON response. This is the default response class in FastAPI.JSONResponse directly, the content must be JSON-serializable. FastAPI’s automatic response serialization using Pydantic models is often more convenient:HTMLResponse
HTMLResponse
PlainTextResponse
PlainTextResponse
RedirectResponse
RedirectResponse
StreamingResponse
StreamingResponse
StreamingResponse
Streams response content. Useful for large files, real-time data, or generated content.Constructor Parameters:Iterator[bytes] | AsyncIterator[bytes]
required
An iterator or async iterator that yields bytes.
int
default:"200"
HTTP status code.
dict[str, str] | None
default:"None"
HTTP headers.
str | None
default:"None"
Media type (Content-Type).
FileResponse
FileResponse
FileResponse
Returns a file as the response. Automatically handles file streaming and sets appropriate headers.Constructor Parameters:str | Path
required
Path to the file.
int
default:"200"
HTTP status code.
dict[str, str] | None
default:"None"
Additional headers.
str | None
default:"None"
Media type. If not set, it will be guessed from the file extension.
str | None
default:"None"
Filename for the Content-Disposition header.
EventSourceResponse
EventSourceResponse