new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · Reference · all subjects

request and response classes

18 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

FastAPI optional dependencies for additional response types

Additional optional dependencies: orjson is required for ORJSONResponse, ujson is required for UJSONResponse.

FastAPI returns JSONResponse by default

By default, FastAPI delivers responses as JSONResponse and inserts the content returned from a path operation into this JSONResponse.

Use JSONResponse to return additional status codes

To return additional status codes beyond the main status code, import JSONResponse and return your content directly by setting the desired status_code parameter. This allows a single path operation to return different status codes, such as 200 OK for updates and 201 Created for new items.

Returning Response directly bypasses serialization

When you return a Response directly, such as JSONResponse, it is returned directly without serialization with a model. Ensure it contains the desired data and that values are valid JSON when using JSONResponse.

JSONResponse available from starlette.responses or fastapi.responses

JSONResponse can be imported either from starlette.responses or from fastapi.responses. FastAPI provides the same starlette.responses as a convenience for developers.

Response class without media type documentation

If you use a Response class without a media type, FastAPI expects that your Response has no content and will not document the response format in the generated OpenAPI documentation.

Response class parameters

The Response class accepts the following parameters: content (a str or bytes), status_code (an int HTTP status code), headers (a dict of strings), and media_type (a str indicating the media type, e.g. 'text/html'). FastAPI automatically adds a Content-Length header and a Content-Type header based on the media_type, and for text types adds a charset.

HTMLResponse class

HTMLResponse takes text or bytes and returns an HTML response.

PlainTextResponse class

PlainTextResponse takes text or bytes and returns a plain text response.

JSONResponse class

JSONResponse takes some data and returns an application/json-encoded response. It is the standard response used in FastAPI.

RedirectResponse default status code

RedirectResponse returns an HTTP redirect. It uses status code 307 (Temporary Redirect) by default.

StreamingResponse class

StreamingResponse takes an async generator or a normal generator/iterator (a function with yield) and streams the response body.

FileResponse class parameters

FileResponse takes the following parameters: path (the file path to stream), headers (custom headers to include as a dictionary), media_type (a string indicating the media type; if not set, inferred from filename or path), and filename (if set, will be included in the Content-Disposition of the response). FileResponse includes appropriate Content-Length, Last-Modified, and ETag headers.

Custom Response class implementation

You can create your own custom Response class by inheriting from Response. The key method to implement is Response.render(content), which must return the content as bytes.

Response directly returned not documented in OpenAPI

A Response returned directly from a path operation function is not documented in OpenAPI (for example, the Content-Type is not documented) and is not visible in the automatic interactive documentation.

Response classes available via fastapi.responses

FastAPI provides the same starlette.responses also via fastapi.responses as a convenience. Most available responses come directly from Starlette.

Request object attributes: scope and receive

A Request object has two key attributes: request.scope, which is a Python dictionary containing metadata associated with the request, and request.receive, which is a function that receives the request body. Both scope and receive are part of the ASGI specification and are required to create a new Request instance.

Accessing request body in exception handlers

You can access the request body in an exception handler by wrapping request handling in a try/except block. When an exception occurs, the Request instance remains in scope, allowing you to read the request body and use it in error handling.

Give your agent this brain