Trait poem::web::FromRequest
source · pub trait FromRequest<'a>: Sized {
// Required method
fn from_request<'life0, 'async_trait>(
req: &'a Request,
body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where Self: 'async_trait,
'a: 'async_trait,
'life0: 'async_trait;
// Provided method
fn from_request_without_body<'async_trait>(
req: &'a Request
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where Self: Send + 'async_trait,
'a: 'async_trait { ... }
}
Expand description
Represents an type that can be extract from requests.
Provided Implementations
-
Option<T>
Extracts
T
from the incoming request, returnsNone
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
orRedisSession
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);
#[poem::async_trait]
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§
sourcefn from_request<'life0, 'async_trait>(
req: &'a Request,
body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
Self: 'async_trait,
'a: 'async_trait,
'life0: '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 Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,
Extract from request head and body.
Provided Methods§
sourcefn from_request_without_body<'async_trait>(
req: &'a Request
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
Self: Send + 'async_trait,
'a: 'async_trait,
fn from_request_without_body<'async_trait>( req: &'a Request ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where Self: Send + 'async_trait, 'a: 'async_trait,
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§
Implementations on Foreign Types§
source§impl<'a> FromRequest<'a> for String
impl<'a> FromRequest<'a> for String
fn from_request<'life0, 'async_trait>( _req: &'a Request, body: &'life0 mut RequestBody ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,
source§impl<'a> FromRequest<'a> for Vec<u8>
impl<'a> FromRequest<'a> for Vec<u8>
fn from_request<'life0, 'async_trait>( _req: &'a Request, body: &'life0 mut RequestBody ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,
source§impl<'a> FromRequest<'a> for Bytes
impl<'a> FromRequest<'a> for Bytes
fn from_request<'life0, 'async_trait>( _req: &'a Request, body: &'life0 mut RequestBody ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,
source§impl<'a, T: FromRequest<'a>> FromRequest<'a> for Option<T>
impl<'a, T: FromRequest<'a>> FromRequest<'a> for Option<T>
fn from_request<'life0, 'async_trait>( req: &'a Request, body: &'life0 mut RequestBody ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,
Implementors§
impl<'a> FromRequest<'a> for &'a HeaderMap
impl<'a> FromRequest<'a> for &'a Uri
impl<'a> FromRequest<'a> for &'a Session
session
only.impl<'a> FromRequest<'a> for &'a Request
impl<'a> FromRequest<'a> for &'a CookieJar
cookie
only.impl<'a> FromRequest<'a> for &'a CsrfToken
impl<'a> FromRequest<'a> for &'a CsrfVerifier
impl<'a> FromRequest<'a> for &'a LocalAddr
impl<'a> FromRequest<'a> for &'a RemoteAddr
impl<'a> FromRequest<'a> for Method
impl<'a> FromRequest<'a> for Version
impl<'a> FromRequest<'a> for Locale
i18n
only.impl<'a> FromRequest<'a> for Body
impl<'a> FromRequest<'a> for Cookie
cookie
only.impl<'a> FromRequest<'a> for Accept
impl<'a> FromRequest<'a> for Multipart
impl<'a> FromRequest<'a> for RealIp
impl<'a> FromRequest<'a> for StaticFileRequest
impl<'a> FromRequest<'a> for TempFile
impl<'a> FromRequest<'a> for WebSocket
websocket
only.