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},
test::TestClient,
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 cli = TestClient::new(app);
let resp = cli.get("/users/100/team/300").send().await;
resp.assert_status_is_ok();
resp.assert_text("100:300").await;
If the path contains only one parameter, then you can omit the tuple.
use poem::{
get, handler,
http::{StatusCode, Uri},
test::TestClient,
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 cli = TestClient::new(app);
let resp = cli.get("/users/100").send().await;
resp.assert_status_is_ok();
resp.assert_text("100").await;
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},
test::TestClient,
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 cli = TestClient::new(app);
let resp = cli.get("/users/foo/team/100").send().await;
resp.assert_status_is_ok();
resp.assert_text("foo:100").await;
Tuple Fields
0: T
Trait Implementations
sourceimpl<'a, T: DeserializeOwned> FromRequest<'a> for Path<T>
impl<'a, T: DeserializeOwned> FromRequest<'a> for Path<T>
sourcefn 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,
Extract from request head and body.
impl<T: Eq> Eq for Path<T>
impl<T> StructuralEq for Path<T>
impl<T> StructuralPartialEq for Path<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Path<T>where
T: RefUnwindSafe,
impl<T> Send for Path<T>where
T: Send,
impl<T> Sync for Path<T>where
T: Sync,
impl<T> Unpin for Path<T>where
T: Unpin,
impl<T> UnwindSafe for Path<T>where
T: UnwindSafe,
Blanket Implementations
impl<T, A, P> Access<T> for Pwhere
A: Access<T>,
P: Deref<Target = A>,
impl<T, A, P> Access<T> for Pwhere
A: Access<T>,
P: Deref<Target = A>,
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, A> DynAccess<T> for Awhere
A: Access<T>,
<A as Access<T>>::Guard: 'static,
impl<T, A> DynAccess<T> for Awhere
A: Access<T>,
<A as Access<T>>::Guard: 'static,
fn load(&self) -> DynGuard<T>
fn load(&self) -> DynGuard<T>
The equivalent of [
Access::load
].sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.