HttpRequest

Struct HttpRequest 

Source
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: Extensions

Type-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

Source

pub fn new(method: String, path: String) -> Self

Source

pub fn with_extensions_capacity( method: String, path: String, capacity: usize, ) -> Self

Create a new request with pre-allocated extensions capacity.

Source

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.

Source

pub fn set_body_bytes(&mut self, bytes: Bytes)

Set the body using Bytes (zero-copy).

This avoids copying the body data from Hyper.

Source

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.

Source

pub fn body_ref(&self) -> &[u8]

Get a reference to the body bytes.

Returns a reference to the body data without copying.

Source

pub fn request_body(&self) -> RequestBody

Get the body as a RequestBody (zero-copy wrapper).

Source

pub fn has_bytes_body(&self) -> bool

Check if the body is using zero-copy Bytes storage.

Source

pub fn set_body(&mut self, body: Vec<u8>)

Set the body from a Vec.

Source

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).

Source

pub fn insert_extension<T: Send + Sync + 'static>(&mut self, value: T)

Insert a typed value into request extensions.

Use this to pass application state to handlers.

§Example
let mut request = HttpRequest::new("GET".into(), "/".into());
request.insert_extension(app_state);
Source

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.

Source

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.

Source

pub fn extension_arc<T: Send + Sync + 'static>(&self) -> Option<Arc<T>>

Get an Arc reference to a typed extension.

Source

pub fn json<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>

Parse the request body as JSON.

With the simd-json feature enabled, this uses SIMD-accelerated parsing which can be 2-3x faster on modern x86_64 CPUs.

§Example
let user: CreateUser = request.json()?;
Source

pub fn form<T: for<'de> Deserialize<'de>>(&self) -> Result<T, Error>

Parse URL-encoded form data

Source

pub fn form_map(&self) -> Result<HashMap<String, String>, Error>

Parse URL-encoded form data into a HashMap

Source

pub fn multipart(&self) -> Result<Vec<FormField>, Error>

Parse multipart form data

Source

pub fn param(&self, name: &str) -> Option<&String>

Get a path parameter by name

Source

pub fn query(&self, name: &str) -> Option<&String>

Get a query parameter by name

Trait Implementations§

Source§

impl Clone for HttpRequest

Source§

fn clone(&self) -> HttpRequest

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HttpRequest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromHttpRequest for HttpRequest

Source§

fn from_http_request(req: Request<Bytes>) -> Self

Create from http crate Request.
Source§

impl FromRequest for HttpRequest

Source§

fn from_request(request: &HttpRequest) -> Result<Self, Error>

Extract data from the request
Source§

impl IntoHttpRequest for HttpRequest

Source§

fn into_http_request(self) -> Request<Bytes>

Convert to http crate Request.
Source§

impl<H, Fut, E1> Service<HttpRequest> for ExtractorHandlerService<H, (E1,)>
where H: Fn(E1) -> Fut + Clone + Send + Sync + 'static, Fut: Future<Output = Result<HttpResponse, Error>> + Send + 'static, E1: Extract + Send + 'static,

Source§

type Response = HttpResponse

Response type.
Source§

type Error = Error

Error type.
Source§

type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Error>> + Send>>

Future type.
Source§

fn call(&self, req: HttpRequest) -> Self::Future

Process a request.
Source§

impl<H, Fut> Service<HttpRequest> for HandlerService<H, ()>
where H: Fn() -> Fut + Clone + Send + Sync + 'static, Fut: Future<Output = Result<HttpResponse, Error>> + Send + 'static,

Source§

type Response = HttpResponse

Response type.
Source§

type Error = Error

Error type.
Source§

type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Error>> + Send>>

Future type.
Source§

fn call(&self, _req: HttpRequest) -> Self::Future

Process a request.
Source§

impl<H, Fut> Service<HttpRequest> for HandlerService<H, (HttpRequest,)>
where H: Fn(HttpRequest) -> Fut + Clone + Send + Sync + 'static, Fut: Future<Output = Result<HttpResponse, Error>> + Send + 'static,

Source§

type Response = HttpResponse

Response type.
Source§

type Error = Error

Error type.
Source§

type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Error>> + Send>>

Future type.
Source§

fn call(&self, req: HttpRequest) -> Self::Future

Process a request.
Source§

impl<S> Service<HttpRequest> for LoggingService<S>
where S: Service<HttpRequest, Response = HttpResponse, Error = Error> + Clone + Send + Sync + 'static, S::Future: Send,

Source§

type Response = HttpResponse

Response type.
Source§

type Error = Error

Error type.
Source§

type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Error>> + Send>>

Future type.
Source§

fn call(&self, req: HttpRequest) -> Self::Future

Process a request.
Source§

impl<S> Service<HttpRequest> for RequestIdService<S>
where S: Service<HttpRequest, Response = HttpResponse, Error = Error> + Clone + Send + Sync + 'static, S::Future: Send,

Source§

type Response = HttpResponse

Response type.
Source§

type Error = Error

Error type.
Source§

type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Error>> + Send>>

Future type.
Source§

fn call(&self, req: HttpRequest) -> Self::Future

Process a request.
Source§

impl<S> Service<HttpRequest> for TimeoutService<S>
where S: Service<HttpRequest, Response = HttpResponse, Error = Error> + Clone + Send + Sync + 'static, S::Future: Send,

Source§

type Response = HttpResponse

Response type.
Source§

type Error = Error

Error type.
Source§

type Future = Pin<Box<dyn Future<Output = Result<HttpResponse, Error>> + Send>>

Future type.
Source§

fn call(&self, req: HttpRequest) -> Self::Future

Process a request.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Injectable for T
where T: Send + Sync + 'static,

Source§

fn type_id_of() -> TypeId
where Self: Sized,

Returns the TypeId of this type (for internal use)
Source§

fn type_name_of() -> &'static str
where Self: Sized,

Returns the type name for debugging
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> Provider for T
where T: Injectable,