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},
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 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.
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.