The HTTP method of the request is available at
curl -X POST "http://localhost:8000"
req.method # 'POST'
The full URL of the request is available as
req.url # "http://localhost:8000/items?limit=10"
It is a string-like object that also exposes individual parts:
req.url.path # "/items" req.url.port # 8000 req.url.scheme # "http" req.url.hostname # "127.0.0.1" req.url.query # "limit=10" req.url.is_secure # False
Request headers are available at
req.headers, an immutable, case-insensitive
curl -H "X-Framework: Bocadillo" "http://localhost:8000"
req.headers['X-Framework'] # "Bocadillo" req.headers['x-framework'] # "Bocadillo"
Query parameters are available at
req.query_params, an immutable
MultiDict, i.e. a dictionary-like object that supports having multiple elements per key.
req.query_params["limit"] # "10" req.query_params.get("notpresent") # None req.query_params["contains"] # "foo" (first item) req.query_params.getlist("contains") # ["foo", "bar"]
Query parameters can be automatically injected based on view parameters. See Routing.
In Bocadillo, the request body is an awaitable. You can retrieve it in several ways, depending on the expected encoding:
|Body type||Invocation||Return type|
|Form data*|| |
- It is safe to access the request body multiple times — it is cached on the first access.
- If JSON is malformed, a
400 Bad Requesterror response is returned.
- Form data contains both form data and multipart (upload) data.
MultiDictis a dictionary-like object that behaves similarly to
It is possible to process the request as a stream of bytes chunks.
To do so, iterate (asynchronously) over the request object itself:
async for chunk in req: # Process chunk text_chunk: str = chunk.decode() # ...
This is useful when the request body may be too large to be loaded in full in memory, or to implement HTTP streaming, e.g. receiving and processing an unbounded stream of data during a single HTTP session.
The request's stream cannot be consumed more than once. If you try to do so, a
RuntimeError will be raised.