Skip to main content

Module extract

Module extract 

Source
Available on crate feature http only.
Expand description

Getting a request’s body into your own types.

A handler should receive one Context carrying everything the request turned into, not a widening tuple of extractors. FromRequest is where you say how that happens: read the body however you like, produce your type, and the generated routes attach it to the context for you.

impl FromRequest<App> for NewTodo {
    async fn from_request(req: Request, app: &App, ctx: &Context) -> Result<Self, Errors> {
        // The JSON:API path: validate against the resource's schema, then
        // map the document. `validated` does the first half.
        let doc = validated(req, ctx, app.validator("todo")).await?;
        Ok(NewTodo { message: doc["data"]["attributes"]["message"].as_str()… })
    }
}

Nothing here assumes JSON. A resource whose create body is multipart, or form-encoded, or a raw upload, implements the same trait and reads the request its own way.

Structs§

JsonlLinesstreaming
A request body, split into lines as it arrives.
Upload
A raw request body — an uploaded file, rather than a JSON:API document.

Constants§

MAX_BODY
The largest body body_text will read: 2 MiB.
MAX_LINEstreaming
The largest single line JsonlLines will read: 1 MiB.
MAX_UPLOAD
The largest Upload read by default: 10 MiB.

Traits§

FromRequest
Build Self from a request.

Functions§

body_text
Read a request body as UTF-8 text.
check_media_type
Check that a request carries a JSON:API body.
path_id
The id a document addresses, from the request path.
validatedvalidation
Read a body and check it against a resource’s request schema.