pub struct Request { /* private fields */ }
Expand description
Represents an HTTP request.
Implementations
Creates a new Request
with the given components parts and body.
Creates a request builder.
Sets the HTTP method for this request.
Returns a reference to the associated original URI.
Sets the version for this request.
Returns a mutable reference to the associated header map.
Returns the string value of the specified header.
NOTE: Returns None
if the header value is not a valid UTF8 string.
Returns the raw path parameter with the specified name
.
Deserialize path parameters.
See also Path
Example
use poem::{
handler,
http::{StatusCode, Uri},
Endpoint, Request, Result, Route,
};
#[handler]
fn index(req: &Request) -> Result<String> {
let (a, b) = req.path_params::<(i32, String)>()?;
Ok(format!("{}:{}", a, b))
}
let app = Route::new().at("/:a/:b", index);
let resp = app
.call(
Request::builder()
.uri(Uri::from_static("/100/abc"))
.finish(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "100:abc");
Deserialize query parameters.
See also Query
Example
use poem::{
handler,
http::{StatusCode, Uri},
Endpoint, Request, Result, Route,
};
use serde::Deserialize;
#[derive(Deserialize)]
struct Params {
a: i32,
b: String,
}
#[handler]
fn index(req: &Request) -> Result<String> {
let params = req.params::<Params>()?;
Ok(format!("{}:{}", params.a, params.b))
}
let app = Route::new().at("/", index);
let resp = app
.call(
Request::builder()
.uri(Uri::from_static("/?a=100&b=abc"))
.finish(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.into_body().into_string().await.unwrap(), "100:abc");
Returns the content type of this request.
Returns a reference to the associated extensions.
Returns a mutable reference to the associated extensions.
Get a reference from extensions, similar to self.extensions().get()
.
Inserts a value to extensions, similar to
self.extensions().insert(data)
.
Returns a reference to the remote address.
Returns a reference to the local address.
This is supported on crate feature cookie
only.
cookie
only.Returns a reference to the CookieJar
Take the body from this request and sets the body to empty.
Consumes the request returning the head and body parts.
Upgrade the connection and return a stream.
Trait Implementations
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,
Extract from request head and body.
Auto Trait Implementations
impl !RefUnwindSafe for Request
impl !UnwindSafe for Request
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
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