pub struct Request { /* private fields */ }
Expand description
Represents an HTTP request.
Implementations§
source§impl 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},
test::TestClient,
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 cli = TestClient::new(app);
let resp = cli.get("/100/abc").send().await;
resp.assert_status_is_ok();
resp.assert_text("100:abc").await;
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},
test::TestClient,
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 cli = TestClient::new(app);
let resp = cli
.get("/")
.query("a", &100)
.query("b", &"abc")
.send()
.await;
resp.assert_status_is_ok();
resp.assert_text("100:abc").await;
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 Clone + Send + Sync + 'static)
pub fn set_data(&mut self, data: impl Clone + 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.
Available 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 split(self) -> (Request, RequestBody)
pub fn split(self) -> (Request, RequestBody)
Returns the parameters used by the extractor.
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§
source§impl<'a> FromRequest<'a> for &'a Request
impl<'a> FromRequest<'a> for &'a Request
source§async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self>
async fn from_request(req: &'a Request, _body: &mut RequestBody) -> Result<Self>
Auto Trait Implementations§
impl !Freeze for Request
impl !RefUnwindSafe for Request
impl Send for Request
impl Sync for Request
impl Unpin for Request
impl !UnwindSafe for Request
Blanket Implementations§
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> TowerCompatExt for T
impl<T> TowerCompatExt for T
source§fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
fn compat<ResBody, Err, Fut>(self) -> TowerCompatEndpoint<Self>where
ResBody: Body + Send + Sync + 'static,
ResBody::Data: Into<Bytes> + Send + 'static,
ResBody::Error: StdError + Send + Sync + 'static,
Err: Into<Error>,
Self: Service<Request<BoxBody<Bytes, Error>>, Response = Response<ResBody>, Error = Err, Future = Fut> + Clone + Send + Sync + Sized + 'static,
Fut: Future<Output = Result<Response<ResBody>, Err>> + Send + 'static,
tower-compat
only.