Importing
Response Classes
Response
Response
Response
The base response class. Use this for custom responses or when you need full control.Constructor Parameters:The response body content.
HTTP status code.
HTTP headers.
Media type (Content-Type).
Background task to run after sending the response.
The HTTP status code.
Response headers.
The response body.
The media type (Content-Type).
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:An iterator or async iterator that yields bytes.
HTTP status code.
HTTP headers.
Media type (Content-Type).
FileResponse
FileResponse
FileResponse
Returns a file as the response. Automatically handles file streaming and sets appropriate headers.Constructor Parameters:Path to the file.
HTTP status code.
Additional headers.
Media type. If not set, it will be guessed from the file extension.
Filename for the Content-Disposition header.
EventSourceResponse
EventSourceResponse