Trait poem::web::FromRequest[][src]

pub trait FromRequest<'a>: Sized {
    type Error: IntoResponse;
    fn from_request<'life0, 'async_trait>(
        req: &'a Request,
        body: &'life0 mut RequestBody
    ) -> Pin<Box<dyn Future<Output = Result<Self, Self::Error>> + Send + 'async_trait>>
    where
        'a: 'async_trait,
        'life0: 'async_trait,
        Self: 'async_trait
; }
Expand description

Represents an type that can be extract from requests.

Provided Implementations

  • Option<T>

    Extracts T from the incoming request, returns None if it fails.

  • &Request

    Extracts the Request from the incoming request.

  • RemoteAddr

    Extracts the remote peer’s address RemoteAddr from request.

  • LocalAddr

    Extracts the local server’s address LocalAddr from request.

  • Method

    Extracts the Method from the incoming request.

  • Version

    Extracts the Version from the incoming request.

  • &Uri

    Extracts the Uri from the incoming request.

  • &HeaderMap

    Extracts the HeaderMap from the incoming request.

  • Data<&T>

    Extracts the Data from the incoming request.

  • TypedHeader<T>

    Extracts the TypedHeader from the incoming request.

  • Path<T>

    Extracts the Path from the incoming request.

  • Query<T>

    Extracts the Query from the incoming request.

  • Form<T>

    Extracts the Form from the incoming request.

  • Json<T>

    Extracts the Json from the incoming request.

    This extractor will take over the requested body, so you should avoid using multiple extractors of this type in one handler.

  • TempFile

    Extracts the TempFile from the incoming request.

    This extractor will take over the requested body, so you should avoid using multiple extractors of this type in one handler.

  • Multipart

    Extracts the Multipart from the incoming request.

    This extractor will take over the requested body, so you should avoid using multiple extractors of this type in one handler.

  • &CookieJar

    Extracts the CookieJar from the incoming request.

    Requires CookieJarManager middleware.

  • &Session

    Extracts the Session from the incoming request.

    Requires CookieSession or RedisSession middleware.

  • Body

    Extracts the Body from the incoming request.

    This extractor will take over the requested body, so you should avoid using multiple extractors of this type in one handler.

  • String

    Extracts the body from the incoming request and parse it into utf8 String.

    This extractor will take over the requested body, so you should avoid using multiple extractors of this type in one handler.

  • Vec<u8>

    Extracts the body from the incoming request and collect it into Vec<u8>.

    This extractor will take over the requested body, so you should avoid using multiple extractors of this type in one handler.

  • Bytes

    Extracts the body from the incoming request and collect it into Bytes.

    This extractor will take over the requested body, so you should avoid using multiple extractors of this type in one handler.

  • WebSocket

    Ready to accept a websocket WebSocket connection.

Custom extractor

The following is an example of a custom token extractor, which extracts the token from the MyToken header.

use std::fmt::{self, Display, Formatter};

use poem::{
    get, handler, http::StatusCode, Endpoint, Error, FromRequest, Request, RequestBody, Result,
    Route,
};

struct Token(String);

#[poem::async_trait]
impl<'a> FromRequest<'a> for Token {
    type Error = Error;

    async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self> {
        let token = req
            .headers()
            .get("MyToken")
            .and_then(|value| value.to_str().ok())
            .ok_or_else(|| Error::new(StatusCode::BAD_REQUEST).with_reason("missing token"))?;
        Ok(Token(token.to_string()))
    }
}

#[handler]
async fn index(token: Token) {
    assert_eq!(token.0, "token123");
}

let app = Route::new().at("/", get(index));
let _ = index
    .call(Request::builder().header("MyToken", "token123").finish())
    .await;

Associated Types

The error type of this extractor.

If you don’t know what type you should use, you can use Error.

Required methods

Perform the extraction.

Implementations on Foreign Types

Implementors