RequestExtraction extension
Reading values straight off the request.
An extractor is a value so a generator can name one, compose it, and check it. Written by hand that indirection is noise — the handler knows what it wants and it wants it from this request:
Future<Result<Todo, Rejection>> readTodo(Request request) async {
final user = await request.extract<AuthUser>(const BearerAuth());
final id = await request.path<String>('id');
final repository = await request.state<TodoRepository>();
final todo = repository.find(id);
return todo == null || !user.mayActOn(todo.owner)
? const Err(_missing)
: Ok(todo);
}
Every method throws the rejection an extractor produced rather than
returning a Result, so the first failure ends the handler and the verb
builder turns it into the response.
That makes this the wrong tool inside a custom extractor. An extract
has to hand its failure back as Err, because a combinator that tries
several extractors in turn reads the Result to decide whether to try the
next one; a throw sails past it and ends the request. Use the extractor
classes there:
// inside FromRequestParts.extract
switch (await const BearerTokenExtractable().extract(request)) {
case Err(:final error):
return Err(error);
case Ok(value: final token):
...
}
In a handler, throwing is what you want; in an extractor, returning is.
- on
Methods
-
apiKey(
{String header = 'x-api-key', String query = 'api_key', bool allowQuery = true}) → Future< String> -
Available on Request, provided by the RequestExtraction extension
An API key from a header, falling back to the query string. -
basicCredentials(
{String realm = 'restricted'}) → Future< BasicCredentials> -
Available on Request, provided by the RequestExtraction extension
The decoded username and password of anAuthorization: Basicheader. -
bearerToken(
) → Future< String> -
Available on Request, provided by the RequestExtraction extension
The token from anAuthorization: Bearerheader. -
body<
T> (T deserialize(Map< String, Object?> json)) → Future<T> -
Available on Request, provided by the RequestExtraction extension
The JSON object body, decoded withdeserialize. -
bodyList<
T> (T deserialize(Map< String, Object?> json)) → Future<List< T> > -
Available on Request, provided by the RequestExtraction extension
The JSON array body, each element decoded withdeserialize. -
bodyStream(
) → Future< Stream< List< >int> > -
Available on Request, provided by the RequestExtraction extension
The body as an unread stream, for a handler that owns its own limit. -
Available on Request, provided by the RequestExtraction extension
One cookie, coerced toT. A nullableTmakes it optional. -
Available on Request, provided by the RequestExtraction extension
Every cookie at once. -
extract<
T> (FromRequestParts< T> extractor) → Future<T> -
Available on Request, provided by the RequestExtraction extension
Runsextractoragainst this request. -
form(
) → Future< FormMap> -
Available on Request, provided by the RequestExtraction extension
The decoded fields of anapplication/x-www-form-urlencodedbody. -
header(
String name) → Future< String?> -
Available on Request, provided by the RequestExtraction extension
One header, ornullwhen it is absent. -
headerMap(
) → Future< Map< String, String> > -
Available on Request, provided by the RequestExtraction extension
Every header at once, keyed by lower-case name. -
host(
) → Future< String> -
Available on Request, provided by the RequestExtraction extension
TheHostheader this request was addressed to. -
multipart(
) → Future< MultipartForm> -
Available on Request, provided by the RequestExtraction extension
The parts of amultipart/form-databody. -
multipartStream(
{int limit = 64 * 1024 * 1024}) → Future< StreamedMultipart> -
Available on Request, provided by the RequestExtraction extension
Hands the multipart body over a part at a time, without buffering it. -
path<
T> (String name) → Future< T> -
Available on Request, provided by the RequestExtraction extension
The{name}path segment, coerced toT. -
peer(
) → Future< PeerInfo> -
Available on Request, provided by the RequestExtraction extension
The connection's peer address and port. -
queries(
) → Future< Map< String, String> > -
Available on Request, provided by the RequestExtraction extension
Every query pair at once. -
query<
T> (String name) → Future< T> -
Available on Request, provided by the RequestExtraction extension
One query value, coerced toT. -
queryList<
T> (String name) → Future< List< T> > -
Available on Request, provided by the RequestExtraction extension
Every value of a repeated query key, coerced toT. -
rawBody(
) → Future< Uint8List> -
Available on Request, provided by the RequestExtraction extension
The body as raw bytes. -
rawQuery(
) → Future< String?> -
Available on Request, provided by the RequestExtraction extension
The raw, undecoded query string, ornullwhen there is none. -
sessionId(
{String name = 'session'}) → Future< String> -
Available on Request, provided by the RequestExtraction extension
A session identifier from a cookie, as 401 rather than 400 when absent. -
state<
T extends Object> () → Future< T> -
Available on Request, provided by the RequestExtraction extension
The application state of typeTattached withwithState. -
textBody(
) → Future< String> -
Available on Request, provided by the RequestExtraction extension
The body as UTF-8 text. -
validBody<
T extends Validatable> (T deserialize(Map< String, Object?> json)) → Future<T> -
Available on Request, provided by the RequestExtraction extension
The JSON object body, decoded and then checked against its constraints.