pub struct Request { /* private fields */ }
Expand description
Represents an HTTP request.
Implementations
sourceimpl Request
impl Request
sourcepub fn from_parts(parts: RequestParts, body: Body) -> Self
pub fn from_parts(parts: RequestParts, body: Body) -> Self
Creates a new Request
with the given components parts and body.
sourcepub fn builder() -> RequestBuilder
pub fn builder() -> RequestBuilder
Creates a request builder.
sourcepub fn set_method(&mut self, method: Method)
pub fn set_method(&mut self, method: Method)
Sets the HTTP method for this request.
sourcepub fn original_uri(&self) -> &Uri
pub fn original_uri(&self) -> &Uri
Returns a reference to the associated original URI.
sourcepub fn set_version(&mut self, version: Version)
pub fn set_version(&mut self, version: Version)
Sets the version for this request.
sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns a mutable reference to the associated header map.
sourcepub fn header(&self, name: impl AsRef<str>) -> Option<&str>
pub fn header(&self, name: impl AsRef<str>) -> Option<&str>
Returns the string value of the specified header.
NOTE: Returns None
if the header value is not a valid UTF8 string.
sourcepub fn raw_path_param(&self, name: &str) -> Option<&str>
pub fn raw_path_param(&self, name: &str) -> Option<&str>
Returns the raw path parameter with the specified name
.
sourcepub fn path_params<T: DeserializeOwned>(&self) -> Result<T, ParsePathError>
pub fn path_params<T: DeserializeOwned>(&self) -> Result<T, ParsePathError>
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");
sourcepub fn params<T: DeserializeOwned>(&self) -> Result<T, ParseQueryError>
pub fn params<T: DeserializeOwned>(&self) -> Result<T, ParseQueryError>
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");
sourcepub fn content_type(&self) -> Option<&str>
pub fn content_type(&self) -> Option<&str>
Returns the content type of this request.
sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Returns a reference to the associated extensions.
sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Returns a mutable reference to the associated extensions.
sourcepub fn data<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn data<T: Send + Sync + 'static>(&self) -> Option<&T>
Get a reference from extensions, similar to self.extensions().get()
.
sourcepub fn set_data(&mut self, data: impl Send + Sync + 'static)
pub fn set_data(&mut self, data: impl Send + Sync + 'static)
Inserts a value to extensions, similar to
self.extensions().insert(data)
.
sourcepub fn remote_addr(&self) -> &RemoteAddr
pub fn remote_addr(&self) -> &RemoteAddr
Returns a reference to the remote address.
sourcepub fn local_addr(&self) -> &LocalAddr
pub fn local_addr(&self) -> &LocalAddr
Returns a reference to the local address.
This is supported on crate feature cookie
only.
cookie
only.Returns a reference to the CookieJar
sourcepub fn take_body(&mut self) -> Body
pub fn take_body(&mut self) -> Body
Take the body from this request and sets the body to empty.
sourcepub fn into_parts(self) -> (RequestParts, Body)
pub fn into_parts(self) -> (RequestParts, Body)
Consumes the request returning the head and body parts.
sourcepub fn take_upgrade(&self) -> Result<OnUpgrade, UpgradeError>
pub fn take_upgrade(&self) -> Result<OnUpgrade, UpgradeError>
Upgrade the connection and return a stream.
Trait Implementations
sourceimpl<'a> FromRequest<'a> for &'a Request
impl<'a> FromRequest<'a> for &'a Request
sourcefn 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 Send for Request
impl Sync for Request
impl Unpin for Request
impl !UnwindSafe for Request
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more