Skip to main content

HttpRequest

Struct HttpRequest 

Source
pub struct HttpRequest(/* private fields */);
Expand description

An HTTP Request

Implementations§

Source§

impl HttpRequest

Source

pub fn head(&self) -> &RequestHead

This method returns reference to the request head

Source

pub fn uri(&self) -> &Url

Request’s uri.

Source

pub fn method(&self) -> &Method

Read the Request method.

Source

pub fn version(&self) -> Version

Read the Request Version.

Source

pub fn headers(&self) -> &HeaderMap

Returns request’s headers.

Source

pub fn path(&self) -> &str

The target path of this Request.

Source

pub fn query_string(&self) -> &str

The query string in the URL.

E.g., id=10

Source

pub fn io(&self) -> Option<&IoRef>

Io reference for current connection

Source

pub fn peer_addr(&self) -> Option<SocketAddr>

Peer socket address

Peer address is actual socket address, if proxy is used in front of ntex http server, then peer address would be address of this proxy.

Source

pub fn match_info(&self) -> &Path<Url>

Get a reference to the Path parameters.

Params is a container for url parameters. A variable segment is specified in the form {identifier}, where the identifier can be used later in a request handler to access the matched value for that segment.

Source

pub fn extensions(&self) -> Ref<'_, Extensions>

Request extensions

Source

pub fn extensions_mut(&self) -> RefMut<'_, Extensions>

Mutable reference to a the request’s extensions

Source

pub fn url_for<U, I>( &self, name: &str, elements: U, ) -> Result<Url, UrlGenerationError>
where U: IntoIterator<Item = I>, I: AsRef<str>,

Generate url for named resource.

Each element is percent-encoded as one literal URL component before it replaces a dynamic segment.

async fn index(req: HttpRequest) -> HttpResponse {
    let url = req.url_for("foo", &["1", "2", "3"]); // <- generate url for "foo" resource
    HttpResponse::Ok().into()
}

fn main() {
    let app = App::default()
        .service(web::resource("/test/{one}/{two}/{three}")
             .name("foo")  // <- set resource name, then it could be used in `url_for`
             .route(web::get().to(index))
        );
}
Source

pub fn url_for_static(&self, name: &str) -> Result<Url, UrlGenerationError>

Generate url for named resource

This method is similar to HttpRequest::url_for() but it can be used for urls that do not contain variable parts.

Source

pub fn resource_map(&self) -> &ResourceMap

Get a reference to a ResourceMap of current application.

Source

pub fn connection_info(&self) -> Ref<'_, ConnectionInfo>

Get ConnectionInfo for the current request.

This method panics if request’s extensions container is already borrowed.

Source

pub fn app_config(&self) -> &WebAppConfig

App config

Source

pub fn app_state<T: 'static>(&self) -> Option<&T>

Get an application state object stored with WebAppConfig.

Trait Implementations§

Source§

impl Clone for HttpRequest

Source§

fn clone(&self) -> HttpRequest

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Drop for HttpRequest

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<St: State> FromRequest<St> for HttpRequest

It is possible to get HttpRequest as an extractor handler parameter

§Example
use ntex::web::{self, App, HttpRequest};

/// extract `HttpRequest` from request
async fn index(req: HttpRequest) -> String {
   format!("Got thing: {req:?}")
}

fn main() {
    let app = App::default().service(
        web::resource("/users/{first}").route(
            web::get().to(index))
    );
}
Source§

type Error = Infallible

The error returned when extraction fails. Read more
Source§

async fn from_request( _: &St, req: &HttpRequest, _: &mut Payload, ) -> Result<Self, Self::Error>

Creates the extractor value for this request.
Source§

impl HttpMessage for HttpRequest

Source§

fn message_headers(&self) -> &HeaderMap

Returns Request’s headers.

Source§

fn message_extensions(&self) -> Ref<'_, Extensions>

Request extensions

Source§

fn message_extensions_mut(&self) -> RefMut<'_, Extensions>

Mutable reference to a the request’s extensions

Source§

fn content_type(&self) -> &str

Returns the media type from the Content-Type header. Read more
Source§

fn encoding(&self) -> Result<&'static Encoding, ContentTypeError>

Returns the character encoding declared by Content-Type. Read more
Source§

fn mime_type(&self) -> Result<Option<Mime>, ContentTypeError>

Parses the Content-Type header as a MIME type. Read more
Source§

fn chunked(&self) -> Result<bool, DecodeError>

Returns whether Transfer-Encoding contains chunked. Read more
Source§

fn cookies(&self) -> Result<Ref<'_, Vec<Cookie<'static>>>, ParseError>

Parses and caches cookies from the Cookie headers. Read more
Source§

fn cookie(&self, name: &str) -> Option<Cookie<'static>>

Returns the first request cookie with the given name.

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