Module

http.request

Immutable HTTP request.

Frozen metadata with async body access. The request is honest about what it is: received data that doesn't change.

Classes

HtmxDetails 15
Parsed htmx request headers with caching. Attached to ``request.htmx``. Truthy when ``HX-Request``…

Parsed htmx request headers with caching.

Attached torequest.htmx. Truthy when HX-Requestis present, providing typed access to all htmx request headers. Values are computed once and cached for the lifetime of the request.

Attributes

Name Type Description
_headers Headers
_server tuple[str, int] | None
_cache dict[str, str | None]

Methods

boosted 0 bool
True if this request came from an hx-boost enhanced element.
property
def boosted(self) -> bool
Returns
bool
history_restore 0 bool
True if htmx is restoring from history (cache miss on back/forward).
property
def history_restore(self) -> bool
Returns
bool
target 0 str | None
The target element ID from HX-Target header.
property
def target(self) -> str | None
Returns
str | None
trigger 0 str | None
The trigger element ID from HX-Trigger header.
property
def trigger(self) -> str | None
Returns
str | None
trigger_name 0 str | None
The name attribute of the trigger element (HX-Trigger-Name header).
property
def trigger_name(self) -> str | None
Returns
str | None
current_url 0 str | None
The browser's current URL from HX-Current-URL header.
property
def current_url(self) -> str | None
Returns
str | None
current_url_abs_path 0 str | None
The path portion of the browser's current URL. Strips scheme and host when the…
property
def current_url_abs_path(self) -> str | None

The path portion of the browser's current URL.

Strips scheme and host when the origin matches this request's server, returning just the path (+ query + fragment). Returns the full URL unchanged when the origin differs or server info is unavailable.

Returns
str | None
prompt 0 str | None
The user response to hx-prompt (HX-Prompt header).
property
def prompt(self) -> str | None
Returns
str | None
partial 0 str | None
The partial element name from HX-Partial header (htmx 4.0+). Set when the requ…
property
def partial(self) -> str | None

The partial element name from HX-Partial header (htmx 4.0+).

Set when the request originates from an<htmx-partial>element.

Returns
str | None
Internal Methods 3
__init__ 2
def __init__(self, headers: Headers, server: tuple[str, int] | None) -> None
Parameters
Name Type Description
headers
server
__bool__ 0 bool
True if this is an htmx request (HX-Request header is present).
def __bool__(self) -> bool
Returns
bool
_get 1 str | None
Read an htmx header with URI-AutoEncoded decoding and caching.
def _get(self, name: str) -> str | None
Parameters
Name Type Description
name
Returns
str | None
_LazyQueryParams 9
QueryParams that parses on first access.

QueryParams that parses on first access.

Methods

get 2 str | None
def get(self, key: str, default: str | None = None) -> str | None
Parameters
Name Type Description
key
default Default:None
Returns
str | None
get_list 1 list[str]
def get_list(self, key: str) -> list[str]
Parameters
Name Type Description
key
Returns
list[str]
get_int 2 int | None
def get_int(self, key: str, default: int | None = None) -> int | None
Parameters
Name Type Description
key
default Default:None
Returns
int | None
get_bool 2 bool | None
def get_bool(self, key: str, default: bool | None = None) -> bool | None
Parameters
Name Type Description
key
default Default:None
Returns
bool | None
Internal Methods 5
__init__ 1
def __init__(self, raw: bytes) -> None
Parameters
Name Type Description
raw
__getitem__ 1 str
def __getitem__(self, key: str) -> str
Parameters
Name Type Description
key
Returns
str
__contains__ 1 bool
def __contains__(self, key: object) -> bool
Parameters
Name Type Description
key
Returns
bool
__iter__ 0 Iterator[str]
def __iter__(self) -> Iterator[str]
Returns
Iterator[str]
__len__ 0 int
def __len__(self) -> int
Returns
int
_LazyCookies 6
Cookies that parse on first access.

Cookies that parse on first access.

Methods

get 2 str | None
def get(self, key: str, default: str | None = None) -> str | None
Parameters
Name Type Description
key
default Default:None
Returns
str | None
Internal Methods 5
__init__ 1
def __init__(self, cookie_header: str) -> None
Parameters
Name Type Description
cookie_header
__getitem__ 1 str
def __getitem__(self, key: str) -> str
Parameters
Name Type Description
key
Returns
str
__contains__ 1 bool
def __contains__(self, key: object) -> bool
Parameters
Name Type Description
key
Returns
bool
__iter__ 0 Iterator[str]
def __iter__(self) -> Iterator[str]
Returns
Iterator[str]
__len__ 0 int
def __len__(self) -> int
Returns
int
Request 31
An immutable HTTP request. Metadata (method, path, headers, etc.) is frozen at creation. Body is a…

An immutable HTTP request.

Metadata (method, path, headers, etc.) is frozen at creation. Body is accessed asynchronously via.body(), .json(), .form().

Query params and cookies are parsed lazily on first access (not at creation).

Attributes

Name Type Description
method str
path str
headers Headers
query QueryParams | _LazyQueryParams
path_params dict[str, str]
http_version str
server tuple[str, int] | None
client tuple[str, int] | None
cookies Mapping[str, str] | _LazyCookies
request_id str
_receive Receive
_cache dict[str, Any]

Methods

htmx 0 HtmxDetails
Typed, cached htmx request details. Truthy when ``HX-Request`` is present:: …
property
def htmx(self) -> HtmxDetails

Typed, cached htmx request details.

Truthy whenHX-Requestis present::

if request.htmx:
    target = request.htmx.target
Returns
HtmxDetails
is_fragment 0 bool
True if this is an htmx fragment request (HX-Request header).
property
def is_fragment(self) -> bool
Returns
bool
is_history_restore 0 bool
True if htmx is restoring from history (cache miss on back/forward).
property
def is_history_restore(self) -> bool
Returns
bool
is_boosted 0 bool
True if this request came from an hx-boost enhanced element.
property
def is_boosted(self) -> bool
Returns
bool
htmx_target 0 str | None
The target element ID from HX-Target header.
property
def htmx_target(self) -> str | None
Returns
str | None
htmx_trigger 0 str | None
The trigger element ID from HX-Trigger header.
property
def htmx_trigger(self) -> str | None
Returns
str | None
htmx_trigger_name 0 str | None
The name attribute of the trigger element (HX-Trigger-Name header).
property
def htmx_trigger_name(self) -> str | None
Returns
str | None
htmx_current_url 0 str | None
The browser's current URL from HX-Current-URL header.
property
def htmx_current_url(self) -> str | None
Returns
str | None
htmx_current_url_abs_path 0 str | None
The path portion of the browser's current URL.
property
def htmx_current_url_abs_path(self) -> str | None
Returns
str | None
htmx_partial 0 str | None
The partial element name from HX-Partial header (htmx 4.0+).
property
def htmx_partial(self) -> str | None
Returns
str | None
content_type 0 str | None
The Content-Type header value.
property
def content_type(self) -> str | None
Returns
str | None
content_length 0 int | None
The Content-Length header as int.
property
def content_length(self) -> int | None
Returns
int | None
url 0 str
Full request URL (path + query string).
property
def url(self) -> str
Returns
str
body 0 bytes
Read the full request body. Result is cached — the ASGI receive is consumed on…
async
async def body(self) -> bytes

Read the full request body.

Result is cached — the ASGI receive is consumed once, then the same bytes are returned on subsequent calls.

Returns
bytes
stream 0 AsyncGenerator[bytes]
Stream the request body in chunks.
async
async def stream(self) -> AsyncGenerator[bytes]
Returns
AsyncGenerator[bytes]
json 0 Any
Parse the body as JSON.
async
async def json(self) -> Any
Returns
Any
text 0 str
Read the body as text (UTF-8).
async
async def text(self) -> str
Returns
str
form 0 FormData
Parse the body as form data (URL-encoded or multipart). Result is cached — the…
async
async def form(self) -> FormData

Parse the body as form data (URL-encoded or multipart).

Result is cached — the body is read and parsed once, then the sameFormDatais returned on subsequent calls.

Supportsapplication/x-www-form-urlencoded(stdlib) and multipart/form-data (requires pip install chirp[forms]).

Returns
FormData Parsed ``FormData`` implementing ``MultiValueMapping``.
from_asgi 3 Request
Create a Request from an ASGI scope and receive callable. Reuses request_id fr…
classmethod
def from_asgi(cls, scope: Scope, receive: Receive, path_params: dict[str, str] | None = None) -> Request

Create a Request from an ASGI scope and receive callable.

Reuses request_id from scope["extensions"]["request_id"] when Pounce (or another ASGI server) has already set it, avoiding redundant UUID generation.

Parameters
Name Type Description
scope
receive
path_params Default:None
Returns
Request