Trait poem::web::FromRequest

source ·
pub trait FromRequest<'a>: Sized {
    // Required method
    fn from_request(
        req: &'a Request,
        body: &mut RequestBody
    ) -> impl Future<Output = Result<Self>> + Send;

    // Provided method
    fn from_request_without_body(
        req: &'a Request
    ) -> impl Future<Output = Result<Self>> + Send { ... }
}
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.

  • RealIp

    Extracts the remote peer’s real ip address 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.

  • Xml<T>

    Extracts the Xml 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.

  • Locale

    Extracts the Locale from the incoming request.

  • StaticFileRequest

    Ready to accept a static file request StaticFileRequest.

  • Accept

    Extracts the Accept header from the incoming request.

  • PathPattern

    Extracts the matched path pattern from the incoming request.

§Create your own 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, test::TestClient, Endpoint, Error, FromRequest, Request,
    RequestBody, Result, Route,
};

struct Token(String);

impl<'a> FromRequest<'a> for Token {
    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::from_string("missing token", StatusCode::BAD_REQUEST))?;
        Ok(Token(token.to_string()))
    }
}

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

let app = Route::new().at("/", get(index));
let cli = TestClient::new(app);

cli.get("/")
    .header("MyToken", "token123")
    .send()
    .await
    .assert_status_is_ok();

Required Methods§

source

fn from_request( req: &'a Request, body: &mut RequestBody ) -> impl Future<Output = Result<Self>> + Send

Extract from request head and body.

Provided Methods§

source

fn from_request_without_body( req: &'a Request ) -> impl Future<Output = Result<Self>> + Send

Extract from request head.

If you know that this type does not need to extract the body, then you can just use it.

For example Query, Path they only extract the content from the request head, using this method would be more convenient. String,Vec<u8> they extract the body of the request, using this method will cause ReadBodyError error.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'a> FromRequest<'a> for String

source§

async fn from_request(_req: &'a Request, body: &mut RequestBody) -> Result<Self>

source§

impl<'a> FromRequest<'a> for Vec<u8>

source§

async fn from_request(_req: &'a Request, body: &mut RequestBody) -> Result<Self>

source§

impl<'a> FromRequest<'a> for Bytes

source§

async fn from_request(_req: &'a Request, body: &mut RequestBody) -> Result<Self>

source§

impl<'a, T: FromRequest<'a>> FromRequest<'a> for Option<T>

source§

async fn from_request(req: &'a Request, body: &mut RequestBody) -> Result<Self>

Implementors§

source§

impl<'a> FromRequest<'a> for &'a HeaderMap

source§

impl<'a> FromRequest<'a> for &'a Uri

source§

impl<'a> FromRequest<'a> for &'a Session

Available on crate feature session only.
source§

impl<'a> FromRequest<'a> for &'a Request

source§

impl<'a> FromRequest<'a> for &'a CookieJar

Available on crate feature cookie only.
source§

impl<'a> FromRequest<'a> for &'a CsrfToken

source§

impl<'a> FromRequest<'a> for &'a CsrfVerifier

source§

impl<'a> FromRequest<'a> for &'a LocalAddr

source§

impl<'a> FromRequest<'a> for &'a RemoteAddr

source§

impl<'a> FromRequest<'a> for Method

source§

impl<'a> FromRequest<'a> for Version

source§

impl<'a> FromRequest<'a> for Locale

Available on crate feature i18n only.
source§

impl<'a> FromRequest<'a> for Body

source§

impl<'a> FromRequest<'a> for Accept

source§

impl<'a> FromRequest<'a> for Multipart

source§

impl<'a> FromRequest<'a> for RealIp

source§

impl<'a> FromRequest<'a> for StaticFileRequest

source§

impl<'a> FromRequest<'a> for TempFile

source§

impl<'a> FromRequest<'a> for WebSocket

Available on crate feature websocket only.
source§

impl<'a, T: Header> FromRequest<'a> for TypedHeader<T>

source§

impl<'a, T: Send + Sync + 'static> FromRequest<'a> for Data<&'a T>

source§

impl<'a, T: DeserializeOwned> FromRequest<'a> for Form<T>

source§

impl<'a, T: DeserializeOwned> FromRequest<'a> for Json<T>

source§

impl<'a, T: DeserializeOwned> FromRequest<'a> for Path<T>

source§

impl<'a, T: DeserializeOwned> FromRequest<'a> for Query<T>

source§

impl<'a, T: DeserializeOwned> FromRequest<'a> for Xml<T>

source§

impl<'a, T: DeserializeOwned> FromRequest<'a> for Yaml<T>

source§

impl<'a, T: FromRequest<'a>> FromRequest<'a> for Result<T>