logo
pub struct HttpRequest { /* private fields */ }
Expand description

An incoming request.

Implementations

This method returns reference to the request head

Request’s uri.

Read the Request method.

Read the Request Version.

Returns request’s headers.

The target path of this request.

The query string in the URL.

Example: id=10

Returns a reference to the URL parameters container.

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

Percent Encoding and URL Parameters

Because each URL parameter is able to capture multiple path segments, both ["%2F", "%25"] found in the request URI are not decoded into ["/", "%"] in order to preserve path segment boundaries. If a url parameter is expected to contain these characters, then it is on the user to decode them.

The resource definition pattern that matched the path. Useful for logging and metrics.

For example, when a resource with pattern /user/{id}/profile is defined and a call is made to /user/123/profile this function would return Some("/user/{id}/profile").

Returns a None when no resource is fully matched, including default services.

The resource name that matched the path. Useful for logging and metrics.

Returns a None when no resource is fully matched, including default services.

Returns a reference a piece of connection data set in an on-connect callback.

let opt_t = req.conn_data::<PeerCertificate>();

Generates URL for a named resource.

This substitutes in sequence all URL parameters that appear in the resource itself and in parent scopes, if any.

It is worth noting that the characters ['/', '%'] are not escaped and therefore a single URL parameter may expand into multiple path segments and elements can be percent-encoded beforehand without worrying about double encoding. Any other character that is not valid in a URL path context is escaped using percent-encoding.

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

let app = App::new()
    .service(web::resource("/test/{one}/{two}/{three}")
         .name("foo")  // <- set resource name so it can be used in `url_for`
         .route(web::get().to(|| HttpResponse::Ok()))
    );

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.

Get a reference to a ResourceMap of current application.

Peer socket address.

Peer address is the directly connected peer’s socket address. If a proxy is used in front of the Actix Web server, then it would be address of this proxy.

To get client connection information .connection_info() should be used.

Will only return None when called in unit tests.

Get ConnectionInfo for the current request.

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

App config

Get an application data object stored with App::data or App::app_data methods during application configuration.

If App::data was used to store object, use Data<T>:

let opt_t = req.app_data::<Data<T>>();

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

It is possible to get HttpRequest as an extractor handler parameter

Examples

use actix_web::{web, App, HttpRequest};
use serde::Deserialize;

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

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

The associated error which can be returned.

Future that resolves to a Self.

Create a Self from request parts asynchronously.

Create a Self from request head asynchronously. Read more

Type of message payload stream

Read the message headers.

Request’s extensions container

Mutable reference to a the request’s extensions container

Message payload stream

Read the request content type. If request did not contain a Content-Type header, an empty string is returned. Read more

Get content type encoding Read more

Convert the request content type to a known mime type.

Check if request has chunked transfer encoding.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

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

Calls U::from(self).

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

Should always be Self

The resulting type after obtaining ownership.

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

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

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