Ushka

API reference

ushka.http.request

This module defines the Request class for handling incoming HTTP requests.

class Request

Represents an incoming HTTP request within the Ushka framework.

This class provides a comprehensive interface to access various aspects of the HTTP request, including headers, query parameters, cookies, session data, and the request body (as raw bytes, text, JSON, form data, or files).

It handles asynchronous reading of the request body and lazy loading of parsed data for efficiency.

__init__(self, app: Ushka, scope: dict, receive: Callable[[], Coroutine])

Initializes a new Request object.

Parameters

appUshka

The instance of the Ushka application.

scopedict

The ASGI scope dictionary for the incoming request.

receivecallable

The ASGI receive channel callable for receiving request body chunks.

body(self) -> bytes

Reads and returns the raw request body as bytes.

The body is read asynchronously from the ASGI stream. Once read, it is cached for subsequent access within the same request.

Returns

bytes

The full request body as raw bytes.

Raises

HTTPPayloadTooLarge

If the request body size exceeds max_body_size_in_KB.

stream(self) -> AsyncGenerator[bytes, None]

Streams the raw request body as an asynchronous generator of bytes chunks.

This allows processing large request bodies without loading the entire content into memory at once. If the body has already been read and cached by a previous call to body(), it will yield the cached content.

Yields

bytes

Chunks of the request body.

text(self) -> str

Reads and returns the request body as decoded text.

The body is first read as bytes (if not already cached) and then decoded using UTF-8.

Returns

str

The full request body as a string.

Raises

HTTPPayloadTooLarge

If the request body size exceeds max_body_size_in_KB.

UnicodeDecodeError

If the request body cannot be decoded as UTF-8.

json(self) -> Any

Reads and returns the request body as parsed JSON.

The body is first read as bytes (if not already cached) and then parsed as a JSON object.

Returns

Any

The parsed JSON content of the request body. The type depends on the JSON structure (e.g., dict, list, str, int, etc.).

Raises

HTTPPayloadTooLarge

If the request body size exceeds max_body_size_in_KB.

json.JSONDecodeError

If the request body contains invalid JSON.

form(self) -> Dict[str, Any]

Reads and returns the request body as parsed form data.

This method handles both application/x-www-form-urlencoded and multipart/form-data content types. For multipart data, it uses parse_multipart_asgi to extract fields and files. Files are stored separately and can be accessed via request.files().

Returns

Dict[str, Any]

A dictionary of form fields. For application/x-www-form-urlencoded, values are automatically unwrapped from lists if there's only one.

Raises

HTTPPayloadTooLarge

If the request body size exceeds max_body_size_in_KB.

HTTPBadRequest

If parsing multipart data fails (e.g., missing boundary).

HTTPPayloadTooLarge

If any form field or file in multipart data exceeds size limits.

files(self) -> Dict[str, Any]

Reads and returns any uploaded files from the request body.

This method implicitly calls request.form() if form data (including files) has not yet been parsed. It extracts file-specific information and SpooledTemporaryFile objects.

Returns

Dict[str, Any]

A dictionary where keys are file input names and values are dictionaries containing 'filename', 'content_type', and a SpooledTemporaryFile object.

Raises

HTTPPayloadTooLarge

If the request body size exceeds max_body_size_in_KB.

HTTPBadRequest

If parsing multipart data fails (e.g., missing boundary).

HTTPPayloadTooLarge

If any uploaded file exceeds its configured size limit.