Trait poem::web::FromRequest [−][src]
pub trait FromRequest<'a>: Sized {
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;
}
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. -
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.
-
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 poem::{handler, route, Endpoint, Error, FromRequest, Request, RequestBody};
struct Token(String);
#[poem::async_trait]
impl<'a> FromRequest<'a> for Token {
async fn from_request(req: &'a Request, body: &mut RequestBody) -> poem::Result<Self> {
let token = req
.headers()
.get("MyToken")
.and_then(|value| value.to_str().ok())
.ok_or_else(|| Error::bad_request("missing token"))?;
Ok(Token(token.to_string()))
}
}
#[handler]
async fn index(token: Token) {
assert_eq!(token.0, "token123");
}
let mut app = route();
app.at("/").get(index);
let _ = index
.call(Request::builder().header("MyToken", "token123").finish())
.await;
Required methods
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,
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,
Perform the extraction.