1
0
mirror of https://github.com/actix/actix-website synced 2024-11-27 10:02:57 +01:00

improve path docs

This commit is contained in:
Rob Ede 2022-04-07 15:54:55 +01:00
parent 12cbd2ebbf
commit b11a2f6e99
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 11 additions and 12 deletions

View File

@ -1,6 +1,7 @@
{
"cSpell.words": [
"actix",
"rustls"
"rustls",
"serde"
]
}

View File

@ -6,7 +6,7 @@ weight: 170
# Type-safe information extraction
Actix Web provides a facility for type-safe request information access called _extractors_ (i.e., `impl FromRequest`). By default, actix-web provides several extractor implementations.
Actix Web 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](https://actix.rs/actix-web/actix_web/trait.FromRequest.html#implementors)).
An extractor can be accessed as an argument to a handler function. Actix Web supports up to 12 extractors per handler function. Argument position does not matter.
@ -14,17 +14,17 @@ An extractor can be accessed as an argument to a handler function. Actix Web sup
# Path
[_Path_][pathstruct] provides information that can be extracted from the Request's path. You can deserialize any variable segment from the path.
[_Path_][pathstruct] 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 into a `tuple`, i.e. `Path<(u32, String)>` or any structure that implements the `Deserialize` trait from the _serde_ crate.
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)>`).
{{< include-example example="extractors" file="path_one.rs" section="path-one" >}}
It is also possible to extract path information to a specific type that implements the `Deserialize` trait from _serde_. Here is an equivalent example that uses _serde_ instead of a _tuple_ type.
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.
{{< include-example example="extractors" file="path_two.rs" section="path-two" >}}
It is also possible to `get` or `query` the request for path parameters by name:
As a non-type-safe alternative, it's also possible to `query` the request for path parameters by name within a handler:
{{< include-example example="extractors" file="path_three.rs" section="path-three" >}}

View File

@ -1,5 +1,5 @@
// <path-one>
use actix_web::{get, web, Result};
use actix_web::{get, web, App, HttpServer, Result};
/// extract path info from "/users/{user_id}/{friend}" url
/// {user_id} - deserializes to a u32
@ -12,8 +12,6 @@ async fn index(path: web::Path<(u32, String)>) -> Result<String> {
#[actix_web::main]
async fn main() -> std::io::Result<()> {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().service(index))
.bind(("127.0.0.1", 8080))?
.run()

View File

@ -1,12 +1,12 @@
use actix_web::{get, HttpRequest, Result};
// <path-three>
#[get("/users/{userid}/{friend}")] // <- define path parameters
#[get("/users/{user_id}/{friend}")] // <- define path parameters
async fn index(req: HttpRequest) -> Result<String> {
let name: String = req.match_info().get("friend").unwrap().parse().unwrap();
let userid: i32 = req.match_info().query("userid").parse().unwrap();
let userid: i32 = req.match_info().query("user_id").parse().unwrap();
Ok(format!("Welcome {}, userid {}!", name, userid))
Ok(format!("Welcome {}, user_id {}!", name, userid))
}
#[actix_web::main]