1
0
mirror of https://github.com/actix/actix-website synced 2024-11-27 18:12:57 +01:00
actix-website/content/docs/extractors.md

134 lines
5.6 KiB
Markdown
Raw Normal View History

2018-05-22 23:15:08 +02:00
---
title: Extractors
menu: docs_basics
weight: 170
---
# Type-safe information extraction
2019-06-20 10:20:12 +02:00
Actix-web provides a facility for type-safe request information access called *extractors*
(ie, `impl FromRequest`). By default, actix-web provides several extractor implementations.
2018-05-22 23:15:08 +02:00
2019-06-20 10:20:12 +02:00
## Extractors Within Handler Functions
2018-05-25 21:51:16 +02:00
2019-06-20 10:20:12 +02:00
An extractor can be accessed in a few different ways.
2018-05-25 21:51:16 +02:00
2019-06-20 10:20:12 +02:00
Option 1 - passed as a parameter to a handler function:
2018-05-25 21:51:16 +02:00
2019-06-20 10:20:12 +02:00
{{< include-example example="extractors" file="main.rs" section="option-one" >}}
2018-05-25 21:51:16 +02:00
2019-06-20 10:20:12 +02:00
Option 2 - accessed by calling `extract()` on the Extractor
2018-05-25 21:51:16 +02:00
2019-06-20 10:20:12 +02:00
{{< include-example example="extractors" file="main.rs" section="option-two" >}}
2018-05-25 21:51:16 +02:00
2018-05-22 23:15:08 +02:00
# Path
2019-06-20 10:20:12 +02:00
[*Path*](https://docs.rs/actix-web/1.0.2/actix_web/dev/struct.Path.html) provides
information that can be extracted from the Request's path. You can deserialize any
variable segment from the path.
2018-05-22 23:15:08 +02:00
2018-06-01 17:11:25 +02:00
For instance, for resource that registered for the `/users/{userid}/{friend}` path
two segments could be deserialized, `userid` and `friend`. These segments
could be extracted into a `tuple`, i.e. `Path<(u32, String)>` or any structure
that implements the `Deserialize` trait from the *serde* crate.
2018-05-22 23:15:08 +02:00
2019-06-17 05:17:17 +02:00
{{< include-example example="extractors" file="path_one.rs" section="path-one" >}}
2018-05-22 23:15:08 +02:00
It is also possible to extract path information to a specific type that
2018-06-01 17:11:25 +02:00
implements the `Deserialize` trait from *serde*. Here is an equivalent example that uses *serde*
instead of a *tuple* type.
2018-05-22 23:15:08 +02:00
2019-06-17 05:17:17 +02:00
{{< include-example example="extractors" file="path_two.rs" section="path-two" >}}
2018-05-22 23:15:08 +02:00
2019-06-20 10:20:12 +02:00
It is also possible to `get` or `query` the request for path parameters by name:
{{< include-example example="extractors" file="path_three.rs" section="path-three" >}}
2018-05-22 23:15:08 +02:00
# Query
2019-06-20 10:20:12 +02:00
The [*Query*](https://docs.rs/actix-web/1.0.2/actix_web/web/struct.Query.html)
type provides extraction functionality for the request's query parameters. Underneath it
uses *serde_urlencoded* crate.
2018-05-22 23:15:08 +02:00
2019-06-17 05:17:17 +02:00
{{< include-example example="extractors" file="query.rs" section="query" >}}
2018-05-22 23:15:08 +02:00
# Json
2019-06-20 10:20:12 +02:00
[*Json*](https://docs.rs/actix-web/1.0.2/actix_web/web/struct.Json.html) allows to deserialize
2018-06-01 17:11:25 +02:00
a request body into a struct. To extract typed information from a request's body,
2018-05-22 23:15:08 +02:00
the type `T` must implement the `Deserialize` trait from *serde*.
2019-06-17 05:17:17 +02:00
{{< include-example example="extractors" file="json_one.rs" section="json-one" >}}
2018-05-22 23:15:08 +02:00
2018-06-01 17:11:25 +02:00
Some extractors provide a way to configure the extraction process. Json extractor
2019-06-20 10:20:12 +02:00
[*JsonConfig*](https://docs.rs/actix-web/1.0.2/actix_web/web/struct.JsonConfig.html) type
for configuration. To configure an extractor, pass it's configuration object to the resource's
`.data()` method. In case of a *Json* extractor it returns a *JsonConfig*. You can
2019-06-20 10:20:12 +02:00
configure the maximum size of the json payload as well as a custom error handler function.
2018-05-22 23:15:08 +02:00
The following example limits the size of the payload to 4kb and uses a custom error handler.
2018-05-22 23:15:08 +02:00
2019-06-17 05:17:17 +02:00
{{< include-example example="extractors" file="json_two.rs" section="json-two" >}}
2018-05-22 23:15:08 +02:00
# Form
2018-06-01 17:11:25 +02:00
At the moment only url-encoded forms are supported. The url-encoded body
2018-05-22 23:15:08 +02:00
could be extracted to a specific type. This type must implement
2018-06-01 17:11:25 +02:00
the `Deserialize` trait from the *serde* crate.
2018-05-22 23:15:08 +02:00
2019-06-20 10:20:12 +02:00
[*FormConfig*](https://docs.rs/actix-web/1.0.2/actix_web/web/struct.FormConfig.html) allows
2018-06-01 17:11:25 +02:00
configuring the extraction process.
2018-05-22 23:15:08 +02:00
2019-06-17 05:17:17 +02:00
{{< include-example example="extractors" file="form.rs" section="form" >}}
2018-05-22 23:15:08 +02:00
# Multiple extractors
2019-06-20 10:20:12 +02:00
Actix-web provides extractor implementations for tuples (up to 10 elements)
2018-06-01 17:11:25 +02:00
whose elements implement `FromRequest`.
2018-05-22 23:15:08 +02:00
2018-06-01 17:11:25 +02:00
For example we can use a path extractor and a query extractor at the same time.
2018-05-22 23:15:08 +02:00
2019-06-17 05:17:17 +02:00
{{< include-example example="extractors" file="multiple.rs" section="multi" >}}
2018-05-22 23:15:08 +02:00
# Other
2019-06-20 10:20:12 +02:00
Actix-web also provides several other extractors:
2018-05-22 23:15:08 +02:00
2019-06-20 10:20:12 +02:00
* [*Data*](https://docs.rs/actix-web/1.0.2/actix_web/web/struct.Data.html) - If you need
access to an application state.
2018-06-01 17:11:25 +02:00
* *HttpRequest* - *HttpRequest* itself is an extractor which returns self,
in case you need access to the request.
* *String* - You can convert a request's payload to a *String*.
2019-06-20 10:20:12 +02:00
[*Example*](https://docs.rs/actix-web/1.0.2/actix_web/trait.FromRequest.html#example-2)
2018-05-22 23:15:08 +02:00
is available in doc strings.
2018-06-01 17:11:25 +02:00
* *bytes::Bytes* - You can convert a request's payload into *Bytes*.
2019-06-20 10:20:12 +02:00
[*Example*](https://docs.rs/actix-web/1.0.2/actix_web/trait.FromRequest.html#example-4)
2018-05-22 23:15:08 +02:00
is available in doc strings.
2019-06-20 10:20:12 +02:00
* *Payload* - You can access a request's payload.
[*Example*](https://docs.rs/actix-web/1.0.2/actix_web/web/struct.Payload.html)
# Async Data Access
Application state is accessible from the handler with the `web::Data` extractor;
however, state is accessible as a read-only reference. If you need mutable access to state,
it must be implemented.
> **Beware**, actix creates multiple copies of the application state and the handlers,
> unique for each thread. If you run your application in several threads, actix will
> create the same amount as number of threads of application state objects and handler
> objects.
Here is an example of a handler that stores the number of processed requests:
{{< include-example example="request-handlers" file="main.rs" section="data" >}}
Although this handler will work, `self.0` will be different depending on the number of threads and
number of requests processed per thread. A proper implementation would use `Arc` and `AtomicUsize`.
{{< include-example example="request-handlers" file="handlers_arc.rs" section="arc" >}}
> Be careful with synchronization primitives like `Mutex` or `RwLock`. The `actix-web` framework
> handles requests asynchronously. By blocking thread execution, all concurrent
> request handling processes would block. If you need to share or update some state
> from multiple threads, consider using the [actix](https://actix.github.io/actix/actix/) actor system.