Skip to main content

Request

Struct Request 

Source
pub struct Request { /* private fields */ }
Expand description

An inbound HTTP request.

Implementations§

Source§

impl Request

Source

pub fn new(method: Method, uri: Uri, headers: HeaderMap, body: Bytes) -> Self

Build a request from its parts. The server calls this; tests should prefer Request::builder.

Source

pub fn builder() -> RequestBuilder

A fluent builder, mainly for tests.

Source

pub fn from_http(request: Request<Bytes>) -> Self

Adopt an http::Request whose body has already been buffered.

Source

pub fn method(&self) -> &Method

The HTTP method.

Source

pub fn uri(&self) -> &Uri

The full request URI.

Source

pub fn path(&self) -> &str

The path, without the query string. Always begins with /.

Source

pub fn query_string(&self) -> &str

The raw query string, without the ?.

Source

pub fn version(&self) -> Version

The HTTP version.

Source

pub fn headers(&self) -> &HeaderMap

Every header.

Source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Mutable access to the headers — for middleware that normalises them.

Source

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

One header as a string, or None if absent or not valid UTF-8.

Source

pub fn set_method(&mut self, method: Method)

Replace the method.

For the one middleware that legitimately needs it: turning a browser form’s POST with _method=DELETE into a real DELETE before routing sees it. See MethodOverride — and note that it must run before the router, or the route has already been chosen for the old method.

Source

pub fn is_method(&self, method: &Method) -> bool

Whether the method is method.

Source

pub fn body(&self) -> &Bytes

The raw body bytes.

Source

pub fn body_string(&self) -> String

The body as UTF-8, lossily.

Source

pub fn content_type(&self) -> Option<String>

The Content-Type header, lowercased and without parameters.

Source

pub fn is_json(&self) -> bool

Whether the body claims to be JSON. Also true for +json suffixes such as application/ld+json.

Source

pub fn expects_json(&self) -> bool

Whether the client would rather have JSON back.

True when it sent JSON, when it asked for JSON in Accept, or when it looks like an XHR. This is what the exception handler consults to decide between a JSON error body and an HTML error page.

Source

pub fn query(&self) -> &Value

The query string parsed into a JSON tree.

Source

pub fn body_input(&self) -> &Value

The body’s fields as a JSON tree — {} when there is no parseable body.

Source

pub fn all(&self) -> Value

Every input: the query string with the body merged over it.

Route parameters are not included — they are part of the URL’s shape rather than user input, and mixing them in would let a query string shadow a route binding. Read them with route_param.

Source

pub fn input(&self, key: &str) -> Option<String>

One input by dotted key, as a string. Body wins over query string.

Source

pub fn transform_input(&mut self, transform: impl FnOnce(Value) -> Value)

Rewrite every input through transform.

The seam input-normalising middleware works through: TrimStrings and ConvertEmptyStringsToNull both take all, reshape it, and hand it back. Later reads see only the rewritten values.

Source

pub fn set_input(&mut self, value: Value)

Replace every input outright.

Source

pub fn merge_input(&mut self, extra: Value)

Merge extra values over the current inputs, for a middleware that resolves something the handler should read as ordinary input.

Source

pub fn input_or(&self, key: &str, default: impl Into<String>) -> String

One input, or default.

Source

pub fn input_value(&self, key: &str) -> Option<&Value>

The raw JSON value at key, for non-scalar inputs.

Source

pub fn has(&self, key: &str) -> bool

Whether key is present at all (even if empty).

Source

pub fn input_was_rewritten(&self) -> bool

Whether input-rewriting middleware has replaced the inputs.

Source

pub fn filled(&self, key: &str) -> bool

Whether key is present and not empty.

Source

pub fn json<T: DeserializeOwned>(&self) -> Result<T>

Deserialise the JSON body into T.

Fails with a 400 when the body is absent, is not JSON, or does not fit T — unlike input, which shrugs at a bad body.

Source

pub fn form<T: DeserializeOwned>(&self) -> Result<T>

Deserialise the merged inputs into T.

Every value from a form or query string is a string, so T should either use string fields or #[serde(deserialize_with = ..)] coercions. For typed coercion from forms, validate instead.

Source

pub fn query_as<T: DeserializeOwned>(&self) -> Result<T>

Deserialise the query string into T.

Source

pub fn files(&self, field: &str) -> &[UploadedFile]

Every file uploaded under field.

Source

pub fn file(&self, field: &str) -> Option<&UploadedFile>

The first file uploaded under field.

Source

pub fn has_file(&self, field: &str) -> bool

Whether a non-empty file arrived under field.

Source

pub fn cookies(&self) -> &HashMap<String, String>

Every cookie the client sent.

Source

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

One cookie’s value.

Source

pub fn bearer_token(&self) -> Option<&str>

The Authorization: Bearer … token, if present.

Source

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

A parameter captured by the matched route (/posts/{post}).

Source

pub fn route_params(&self) -> &HashMap<String, String>

Every captured route parameter.

Source

pub fn set_route_params(&mut self, params: HashMap<String, String>)

Replace the captured route parameters. The router calls this on match.

Source

pub fn extensions(&self) -> &Extensions

Per-request attributes, keyed by type: the authenticated user, the matched route, a resolved model binding.

Source

pub fn extensions_mut(&mut self) -> &mut Extensions

Mutable per-request attributes.

Source

pub fn with_extension<T: Send + Sync + 'static>(self, value: T) -> Self

Store a per-request attribute, returning self for chaining.

Source

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

Read a per-request attribute.

Source

pub fn ip(&self) -> Option<IpAddr>

The client’s IP, if the server recorded one.

Trait Implementations§

Source§

impl Debug for Request

Source§

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

Formats the value using the given formatter. Read more

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.