liveBook · Manning

Request extractor

As explained in Rust Servers, Services, and Apps

A request extractor is an Actix Web component that obtains typed request or application data for a handler.

A request extractor is an Actix Web component that supplies typed request or application data to a handler as an argument. It connects incoming HTTP data and shared application state to request-processing code. section 4.4.2 section 3.2.1

Common Actix Web extractors include:

  • `web::Data<AppState>` provides shared application state, such as a database connection pool, to handlers. section 3.2.1 section 4.4.2
  • `web::Path<(i32, i32)>` extracts numeric path parameters such as `tutor-id` and `course-id` as a tuple. section 4.4.2 section 9.4
  • `web::Form<Tutor>` extracts submitted form data into a typed structure, making the submitted `name` available to the handler. section 7.4

Extracting route parameters

A course-details handler can accept `app_state: web::Data<AppState>` and `params: web::Path<(i32, i32)>`. Read `params.0`, convert the tuple values to the required integer type, pass them with `&app_state.db` to the database function, and serialize the returned course with `HttpResponse::Ok().json(course)`.

Processing submitted form data

A POST handler can accept `params: web::Form<Tutor>`, insert `params.name` into a Tera context, and render `user.html`. A browser form posts the `name` field to `/tutors`.

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains extractor placement, tuple access, type conversion, database use, and template rendering, but omits extractor implementation internals.
Examples
Uses shared state, tutor and course path identifiers, an HTML tutor form, a typed Tutor structure, and Tera-rendered output.
What it assumes
Assumes handler functions and HTTP routes, while demonstrating request data extraction through concrete path, state, and form examples.
What you can do after
Apply it via Actix Web to pass typed request data and shared state into handlers.
How it connects
Ties extractors to handler functions, routes, application state, path parameters, forms, database operations, and HTTP responses.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.