Struct poem::web::Path [−][src]
pub struct Path<T>(pub T);
Expand description
An extractor that will get captures from the URL and parse them using
serde
.
Errors
Example
use poem::{
get, handler,
http::{StatusCode, Uri},
web::Path,
Endpoint, Request, Route,
};
#[handler]
async fn users_teams_show(Path((user_id, team_id)): Path<(String, String)>) -> String {
format!("{}:{}", user_id, team_id)
}
let app = Route::new().at("/users/:user_id/team/:team_id", get(users_teams_show));
let resp = app
.call(
Request::builder()
.uri(Uri::from_static("/users/100/team/300"))
.finish(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "100:300");
If the path contains only one parameter, then you can omit the tuple.
use poem::{
get, handler,
http::{StatusCode, Uri},
web::Path,
Endpoint, Request, Route,
};
#[handler]
async fn user_info(Path(user_id): Path<String>) -> String {
user_id
}
let app = Route::new().at("/users/:user_id", get(user_info));
let resp = app
.call(
Request::builder()
.uri(Uri::from_static("/users/100"))
.finish(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "100");
Path segments also can be deserialized into any type that implements serde::Deserialize
.
Path segment labels will be matched with struct field names.
use poem::{
get, handler,
http::{StatusCode, Uri},
web::Path,
Endpoint, Request, Route,
};
use serde::Deserialize;
#[derive(Deserialize)]
struct Params {
user_id: String,
team_id: String,
}
#[handler]
async fn users_teams_show(Path(Params { user_id, team_id }): Path<Params>) -> String {
format!("{}:{}", user_id, team_id)
}
let app = Route::new().at("/users/:user_id/team/:team_id", get(users_teams_show));
let resp = app
.call(
Request::builder()
.uri(Uri::from_static("/users/foo/team/100"))
.finish(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "foo:100");
Tuple Fields
0: T
Trait Implementations
fn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>> where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'a Request,
_body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>> where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Perform the extraction.
Auto Trait Implementations
impl<T> RefUnwindSafe for Path<T> where
T: RefUnwindSafe,
impl<T> UnwindSafe for Path<T> where
T: UnwindSafe,
Blanket Implementations
impl<T, A, P> Access<T> for P where
A: Access<T>,
P: Deref<Target = A>,
impl<T, A, P> Access<T> for P where
A: Access<T>,
P: Deref<Target = A>,
Mutably borrows from an owned value. Read more
pub fn load(&self) -> DynGuard<T>
pub fn load(&self) -> DynGuard<T>
The equivalent of [Access::load
].
Compare self to key
and return true
if they are equal.
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more