Fields:
method: HTTP method string (use Method.* constants).
url: Request target (path + query), e.g. "/items?page=1".
headers: Request headers (owned HeaderMap).
body: Request body bytes (empty for GET/HEAD).
version: HTTP version string (default "HTTP/1.1").
peer: Kernel-reported peer address. Populated by
the reactor at accept time from TcpStream.peer_addr().
Direct Request constructions (tests, the client side)
default to SocketAddr.localhost(0) so the field is
always observable without having to special-case "did the
reactor populate it." Note: this is the kernel's view
of the peer; flare does not interpret X-Forwarded-For,
Forwarded:, or PROXY-protocol metadata.
expose_errors: When True, 4xx / 5xx responses generated from
a raised Error may include the verbatim message in
their body — useful in local dev. Default False
so production servers send a fixed status reason and
log the message (with any user-controlled bytes) to
stderr rather than echoing it back. Carried on the
request because flare is shared-nothing thread-per-core
and the policy is set per-server (per-ServerConfig);
the reactor copies ServerConfig.expose_error_messages
onto every parsed Request and _bad_request_from_error
reads it.
Path parameters extracted by Router live on a private field that
is lazily allocated on the first Router match. Handlers that
never go through a Router (the plaintext bench, pure static
handlers) therefore pay zero allocation cost per request. Access
params via req.params() or the convenience req.param(name)
/ req.has_param(name) helpers.
This type is Movable (owns the header map and body) but not
Copyable to avoid accidental deep copies.
Example:
var req = Request(method=Method.POST, url="http://api.example.com/items")
req.headers.set("Content-Type", "application/json")
req.body = '{"name":"flare"}'.as_bytes()
__init__
def __init__(out self, method: String, url: String, body: List[UInt8] = List(), version: String = "HTTP/1.1", peer: SocketAddr = SocketAddr(IpAddr(String("127.0.0.1"), False), UInt16(0)), expose_errors: Bool = False)
Create a new HTTP request.
- Args
method: HTTP method string.url: Full URL or request target.body: Request body bytes; empty by default.version: HTTP version; "HTTP/1.1" by default.peer: Peer SocketAddr; defaults to 127.0.0.1:0
for direct constructions. The reactor passes
the kernel-reported peer captured at accept
time.expose_errors: Whether handler / extractor error messages
may flow into the response body. Default
False (production-safe). The reactor
copies this from
ServerConfig.expose_error_messages.
test_post
def test_post(url: String, body: String, content_type: String = "application/octet-stream") -> Self
Construct a POST request with a string body.
Convenience factory for cookbook examples and unit tests
that want to drive an extractor / handler with a synthetic
body without writing the
Request(method="POST", url=url, version="HTTP/1.1") +
per-byte body.append(...) loop the synthetic-request
shape used to require.
- Args
url: Request target (path + optional query string).body: Body bytes as a UTF-8 String. Empty string
is fine.content_type: Value for the Content-Type header.
Defaults to "application/octet-stream"; pass
"application/json" for JSON bodies and
"application/x-www-form-urlencoded" for form
bodies.
body_stream
def body_stream(self) -> Optional[ChunkSourceBox]
The body as a source that yields it as it arrives, or None when the body is already whole in :attr:body. Set it as a response's body_stream to stream the upload back out.
content_length
def content_length(self) -> Int
Return the Content-Length header value, or 0 if absent or not a plain decimal of at most 18 digits.