Requests and responses¶
Request
¶
Immutable view of an incoming HTTP request, handed to the Python handler.
frozen means no Python-side mutation, so no locking is needed even on
free-threaded builds.
body
property
¶
The raw request body. A pydantic-annotated argument is the usual way to read a body; this is for handlers that parse it themselves.
headers
property
¶
Every header, lowercased. Repeated headers are joined with ", " as HTTP itself defines.
query
property
¶
The raw query string, or None. Declared query parameters are already coerced and passed as handler arguments; this is for the rest.
form
method descriptor
¶
Parse the body as a form, application/x-www-form-urlencoded or
multipart/form-data, into a FormData.
Parsed each time it is called, and only when it is called. Raises
HTTPError(415) for a body that is not a form, HTTPError(400) for a
malformed one and HTTPError(413) past max_parts, which bounds how
many Python objects a single request can make the worker build.
header
method descriptor
¶
One header by name, case-insensitively. None if absent.
This is the cheap path: no dict is built, and a header that is not valid UTF-8 reads as absent rather than raising.
stream
method descriptor
¶
The body as an async iterator of bytes chunks: a BodyStream.
On a route that declares a BodyStream argument the chunks arrive as
the client sends them, and nothing is read until the first one is
asked for. On any other route the body was already collected, and this
yields it as a single chunk, so code reading a stream works on both.
Response
dataclass
¶
Response(
body: bytes | str = b"",
status: int = 200,
content_type: str = "application/json",
headers: dict[str, str] = dict(),
)
A ready-to-send response.
body may be bytes or str; str is encoded as UTF-8. headers are sent in
addition to the content type.
encoded
¶
Reply
¶
A handler's result on its way back out.
value is whatever the handler returned: a dict, a model, a Response, an
SSE, or None. status overrides what that value would otherwise imply.
headers are added to the response.
Source code in python/oxbrook/_middleware.py
FormData
¶
Bases: Mapping
Parsed form fields, in the order they arrived.
Indexing returns the first value for a name, as most forms have one; use
getlist for a field that repeats. Text fields are str, files are
UploadFile.
Source code in python/oxbrook/_forms.py
from_parts
classmethod
¶
from_parts(parts: list[tuple]) -> FormData
Source code in python/oxbrook/_forms.py
getlist
¶
UploadFile
¶
One file from a multipart form.
Source code in python/oxbrook/_forms.py
read
¶
Form
¶
Marks a pydantic-model argument as bound from a form body.
async def signup(_: Request, data: Signup = Form()): ...
max_parts bounds how many fields and files one request may send.
Source code in python/oxbrook/_forms.py
BodyStream
¶
The request body, as an async iterator of bytes.
Use as a handler argument's annotation to make a route stream, or call
request.stream(). On a route that did not stream, it yields the
already-collected body once.
Source code in python/oxbrook/_bodies.py
CORS
dataclass
¶
CORS(
allow_origins: Iterable[str],
allow_methods: Iterable[str] = ("*",),
allow_headers: Iterable[str] = ("*",),
allow_credentials: bool = False,
expose_headers: Iterable[str] = tuple(),
max_age: int | None = 600,
)
Which other origins may call this app from a browser.
allow_origins lists exact origins — scheme, host and port as the browser
sends them — or ["*"] for any.
allow_credentials lets the browser send cookies and Authorization on
those requests. It cannot be combined with "*": that would let every
website make authenticated requests as a signed-in user, so it raises here
rather than being quietly made to work.
expose_headers lists response headers a page's script may read, beyond
the few a browser always exposes. max_age is how long, in seconds, a
browser may cache a preflight answer.