logo
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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Extract from request head and body.

Extract from request head. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

A guard object containing the value and keeping it alive. Read more

The loading method. Read more

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

The equivalent of [Access::load].

Compare self to key and return true if they are equal.

Performs the conversion.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more

Attaches the current Context to this type, returning a WithContext wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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