Ushka

API reference

ushka.http.response

This module defines the Response class for handling HTTP responses.

The Response class ensures correct formatting for ASGI compatibility and automatically handles Cookie and Session persistence if a Request object is provided.

class Response

Represents an outgoing HTTP response.

__init__(self, body: str | int | Dict | List | bytes = '', status_code: int = 200, headers: Dict[str, str] | None = None, media_type: str | None = None, request: Optional[Request] = None) -> None

Initializes a new Response object.

Parameters

bodystr, int, dict, list, or bytes

The content for the response body. Can be a string, integer, dictionary (will be JSON-encoded), list (will be JSON-encoded), or raw bytes. Defaults to an empty string.

status_codeint

The HTTP status code for the response. Defaults to 200.

headersDict[str, str]

A dictionary of custom HTTP headers to include in the response. Defaults to None.

media_typestr

The explicit MIME type for the response (e.g., "text/html", "application/json"). If None, the media type is inferred from the body content.

requestRequest

An optional reference to the incoming Request object. If provided, it's used to automatically persist session data and cookies.