Skip to main content

Type-safe information extraction

ntex provides a facility for type-safe request information access called extractors (i.e., impl FromRequest). There are lots of built-in extractor implementations (see implementors).

An extractor can be accessed as an argument to a handler function. ntex supports up to 16 extractors per handler function. Argument position does not matter.


Path​

Path provides information that is extracted from the request's path. Parts of the path that are extractable are called "dynamic segments" and are marked with curly braces. You can deserialize any variable segment from the path.

For instance, for resource that registered for the /users/{user_id}/{friend} path, two segments could be deserialized, user_id and friend. These segments could be extracted as a tuple in the order they are declared (e.g., Path<(u32, String)>).


It is also possible to extract path information to a type that implements the Deserialize trait from serde by matching dynamic segment names with field names. Here is an equivalent example that uses serde instead of a tuple type.


As a non-type-safe alternative, it's also possible to query (see match_info docs) the request for path parameters by name within a handler:


Query​

The Query<T> type provides extraction functionality for the request's query parameters. Underneath it uses serde_urlencoded crate.


JSON​

Json<T> allows deserialization of a request body into a struct. To extract typed information from a request's body, the type T must implement serde::Deserialize.


Some extractors provide a way to configure the extraction process. To configure an extractor, register its configuration object with WebAppConfig::set_state() and pass the config to App::with_config(). In the case of Json extractor it is a JsonConfig. You can configure the maximum size of the JSON payload as well as a custom error handler function.

The following example limits the size of the payload to 4kb and uses a custom error handler.


URL-Encoded Forms​

A URL-encoded form body can be extracted to a struct, much like Json<T>. This type must implement serde::Deserialize.

FormConfig allows configuring the extraction process.


Other​

ntex also provides many other extractors, here's a few important ones:

  • HttpRequest - HttpRequest is itself an extractor, in case you need access to other parts of the request.
  • String - You can convert a request's payload to a String. An example is available in the rustdoc.
  • Bytes - You can convert a request's payload into Bytes. An example is available in the rustdoc.
  • Payload - Low-level payload extractor primarily for building other extractors. An example is available in the rustdoc.

Application State​

Application state is not an extractor. Handlers registered with to_with_state() receive a reference to the application state as their first argument and the request state as the second one, followed by extractors. State is accessible as a read-only reference. If you need mutable access to state, it must be implemented, for example with Cell or RefCell.

Here is an example of a handler that stores the number of processed requests:


Although this handler will work, state.count will only count the number of requests handled by each worker thread, because the state factory is called for each worker. To count the number of total requests across all threads, one should use shared Arc and atomics.


Note: If you want the entire state to be shared across all threads, create it outside of the state factory as described in Shared Mutable State.

Be careful when using blocking synchronization primitives like Mutex or RwLock within your app state. ntex handles requests asynchronously. It is a problem if the critical section in your handler is too big or contains an .await point. If this is a concern, we would advise you to also read Tokio's advice on using blocking Mutex in async code.