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
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 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>,
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
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 A where
A: Access<T>,
<A as Access<T>>::Guard: 'static,
impl<T, A> DynAccess<T> for A where
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 Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
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.
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more