pub struct HttpRequest {
pub method: String,
pub path: String,
pub headers: HashMap<String, String>,
pub body: Vec<u8>,
pub path_params: HashMap<String, String>,
pub query_params: HashMap<String, String>,
pub extensions: Extensions,
/* private fields */
}Expand description
HTTP request wrapper
The body is stored as Vec<u8> for backwards compatibility.
For zero-copy body handling, use body_bytes() or set_body_bytes().
Fields§
§method: String§path: String§headers: HashMap<String, String>§body: Vec<u8>Request body as raw bytes.
For zero-copy access, use body_bytes() to get a Bytes view,
or use RequestBody for efficient body handling.
path_params: HashMap<String, String>§query_params: HashMap<String, String>§extensions: ExtensionsType-safe extensions for storing application state.
Use this to pass typed data to handlers without DI container lookups.
Access via the State<T> extractor for zero-cost state retrieval.
Implementations§
Source§impl HttpRequest
impl HttpRequest
pub fn new(method: String, path: String) -> Self
Sourcepub fn with_extensions_capacity(
method: String,
path: String,
capacity: usize,
) -> Self
pub fn with_extensions_capacity( method: String, path: String, capacity: usize, ) -> Self
Create a new request with pre-allocated extensions capacity.
Sourcepub fn with_bytes_body(method: String, path: String, body: Bytes) -> Self
pub fn with_bytes_body(method: String, path: String, body: Bytes) -> Self
Create a new request with a Bytes body (zero-copy).
This is the most efficient way to create a request from Hyper’s body, as it avoids copying the body data.
Sourcepub fn set_body_bytes(&mut self, bytes: Bytes)
pub fn set_body_bytes(&mut self, bytes: Bytes)
Set the body using Bytes (zero-copy).
This avoids copying the body data from Hyper.
Sourcepub fn body_bytes(&self) -> Bytes
pub fn body_bytes(&self) -> Bytes
Get the body as Bytes (zero-copy if stored as Bytes).
If the body was set via set_body_bytes() or with_bytes_body(),
this returns a clone of the Bytes (O(1) reference count increment).
Otherwise, it creates Bytes from the Vec
Sourcepub fn body_ref(&self) -> &[u8] ⓘ
pub fn body_ref(&self) -> &[u8] ⓘ
Get a reference to the body bytes.
Returns a reference to the body data without copying.
Sourcepub fn request_body(&self) -> RequestBody
pub fn request_body(&self) -> RequestBody
Get the body as a RequestBody (zero-copy wrapper).
Sourcepub fn has_bytes_body(&self) -> bool
pub fn has_bytes_body(&self) -> bool
Check if the body is using zero-copy Bytes storage.
Sourcepub fn from_parts(
method: String,
path: String,
headers: HashMap<String, String>,
body: Vec<u8>,
path_params: HashMap<String, String>,
query_params: HashMap<String, String>,
) -> Self
pub fn from_parts( method: String, path: String, headers: HashMap<String, String>, body: Vec<u8>, path_params: HashMap<String, String>, query_params: HashMap<String, String>, ) -> Self
Create a request from all parts (for compatibility in tests).
Sourcepub fn insert_extension<T: Send + Sync + 'static>(&mut self, value: T)
pub fn insert_extension<T: Send + Sync + 'static>(&mut self, value: T)
Sourcepub fn insert_extension_arc<T: Send + Sync + 'static>(&mut self, value: Arc<T>)
pub fn insert_extension_arc<T: Send + Sync + 'static>(&mut self, value: Arc<T>)
Insert an Arc-wrapped value into request extensions.
This is more efficient when you already have an Arc.
Sourcepub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T>
Get a reference to a typed extension.
Returns None if no value of this type exists.
Sourcepub fn extension_arc<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
pub fn extension_arc<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>
Get an Arc reference to a typed extension.
Sourcepub fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
pub fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
Sourcepub fn form<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
pub fn form<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>
Parse URL-encoded form data
Trait Implementations§
Source§impl Clone for HttpRequest
impl Clone for HttpRequest
Source§fn clone(&self) -> HttpRequest
fn clone(&self) -> HttpRequest
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more