Struct actix_web::web::HttpRequest
source · [−]pub struct HttpRequest { /* private fields */ }
Expand description
An incoming request.
Implementations
sourceimpl HttpRequest
impl HttpRequest
sourcepub fn head(&self) -> &RequestHead
pub fn head(&self) -> &RequestHead
This method returns reference to the request head
sourcepub fn query_string(&self) -> &str
pub fn query_string(&self) -> &str
The query string in the URL.
Example: id=10
sourcepub fn match_info(&self) -> &Path<Url>
pub fn match_info(&self) -> &Path<Url>
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.
sourcepub fn match_pattern(&self) -> Option<String>
pub fn match_pattern(&self) -> Option<String>
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.
sourcepub fn match_name(&self) -> Option<&str>
pub fn match_name(&self) -> Option<&str>
The resource name that matched the path. Useful for logging and metrics.
Returns a None when no resource is fully matched, including default services.
pub fn req_data(&self) -> Ref<'_, Extensions>
pub fn req_data_mut(&self) -> RefMut<'_, Extensions>
sourcepub fn conn_data<T: 'static>(&self) -> Option<&T>
pub fn conn_data<T: 'static>(&self) -> Option<&T>
Returns a reference a piece of connection data set in an on-connect callback.
let opt_t = req.conn_data::<PeerCertificate>();
sourcepub fn url_for<U, I>(
&self,
name: &str,
elements: U
) -> Result<Url, UrlGenerationError> where
U: IntoIterator<Item = I>,
I: AsRef<str>,
pub fn url_for<U, I>(
&self,
name: &str,
elements: U
) -> Result<Url, UrlGenerationError> where
U: IntoIterator<Item = I>,
I: AsRef<str>,
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()))
);
sourcepub fn url_for_static(&self, name: &str) -> Result<Url, UrlGenerationError>
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.
sourcepub fn resource_map(&self) -> &ResourceMap
pub fn resource_map(&self) -> &ResourceMap
Get a reference to a ResourceMap
of current application.
sourcepub fn peer_addr(&self) -> Option<SocketAddr>
pub fn peer_addr(&self) -> Option<SocketAddr>
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.
sourcepub fn connection_info(&self) -> Ref<'_, ConnectionInfo>
pub fn connection_info(&self) -> Ref<'_, ConnectionInfo>
Get ConnectionInfo for the current request.
This method panics if request’s extensions container is already borrowed.
sourcepub fn app_config(&self) -> &AppConfig
pub fn app_config(&self) -> &AppConfig
App config
Trait Implementations
sourceimpl Clone for HttpRequest
impl Clone for HttpRequest
sourcefn clone(&self) -> HttpRequest
fn clone(&self) -> HttpRequest
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for HttpRequest
impl Debug for HttpRequest
sourceimpl Drop for HttpRequest
impl Drop for HttpRequest
sourceimpl FromRequest for HttpRequest
impl FromRequest for HttpRequest
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))
);
}
type Future = Ready<Result<HttpRequest, Error>>
type Future = Ready<Result<HttpRequest, Error>>
Future that resolves to a Self.
sourcefn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future
Create a Self from request parts asynchronously.
sourcefn extract(req: &HttpRequest) -> Self::Future
fn extract(req: &HttpRequest) -> Self::Future
Create a Self from request head asynchronously. Read more
sourceimpl HttpMessage for HttpRequest
impl HttpMessage for HttpRequest
sourcefn extensions(&self) -> Ref<'_, Extensions>
fn extensions(&self) -> Ref<'_, Extensions>
Request’s extensions container
sourcefn extensions_mut(&self) -> RefMut<'_, Extensions>
fn extensions_mut(&self) -> RefMut<'_, Extensions>
Mutable reference to a the request’s extensions container
sourcefn take_payload(&mut self) -> Payload<Self::Stream>
fn take_payload(&mut self) -> Payload<Self::Stream>
Message payload stream
sourcefn content_type(&self) -> &str
fn content_type(&self) -> &str
Read the request content type. If request did not contain a Content-Type header, an empty string is returned. Read more
sourcefn encoding(&self) -> Result<&'static Encoding, ContentTypeError>
fn encoding(&self) -> Result<&'static Encoding, ContentTypeError>
Get content type encoding Read more
sourcefn mime_type(&self) -> Result<Option<Mime>, ContentTypeError>
fn mime_type(&self) -> Result<Option<Mime>, ContentTypeError>
Convert the request content type to a known mime type.
sourcefn chunked(&self) -> Result<bool, ParseError>
fn chunked(&self) -> Result<bool, ParseError>
Check if request has chunked transfer encoding.
Auto Trait Implementations
impl !RefUnwindSafe for HttpRequest
impl !Send for HttpRequest
impl !Sync for HttpRequest
impl Unpin for HttpRequest
impl !UnwindSafe for HttpRequest
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more