Skip to main content

Request

Struct Request 

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

An HTTP request as seen by middleware, API routes and server actions.

Implementations§

Source§

impl Request

Source

pub fn from_parts( parts: Parts, body: UnsyncBoxBody<Bytes, Box<dyn Error + Send + Sync>>, remote_addr: Option<SocketAddr>, body_limit: usize, secure_cookies: bool, ) -> Request

Source

pub fn from_http(req: Request<Bytes>) -> Request

Build a request from any http::Request with a buffered body (useful in tests and serverless adapters).

Source

pub fn get(uri: &str) -> Request

A body-less GET request for uri (tests, static rendering).

Source

pub fn method(&self) -> &Method

Source

pub fn uri(&self) -> &Uri

Source

pub fn path(&self) -> &str

Source

pub fn query_string(&self) -> &str

Source

pub fn query<T>(&self) -> Result<T, Error>

Deserialize the query string into T.

Source

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

A single query parameter.

Source

pub fn headers(&self) -> &HeaderMap

Source

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

Source

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

Header value as text (None if missing or not visible ASCII).

Source

pub fn params(&self) -> &Params

Source

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

Source

pub fn cookies(&self) -> &Cookies

Source

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

Source

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

The client’s IP address. With [server] trust_proxy, the first X-Forwarded-For entry (the address your proxy saw); otherwise, or when that entry is not an address, the peer’s. Never trust the header without a proxy that overwrites it: clients can send anything.

Source

pub fn extensions(&self) -> &Extensions

Source

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

Source

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

Typed request-scoped value inserted by middleware (e.g. the current user).

Source

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

Source

pub fn set_path(&mut self, path_and_query: &str) -> Result<(), Error>

Rewrite the request path (middleware only; routing happens afterwards).

Source

pub fn set_body_limit(&mut self, limit: usize)

Override the maximum body size for this request.

Source

pub fn is_websocket_upgrade(&self) -> bool

Source

pub fn same_origin(&self) -> bool

true unless the browser says the request came from another site.

Checks Sec-Fetch-Site, then Origin against the request host (X-Forwarded-Host behind a trusted proxy), allowing [security] allowed_origins. Requests without either header (curl, server to server) pass: they carry no ambient cookies across sites. Server actions run this check already; call it yourself in API routes and WebSocket handlers that change state or return private data:

ⓘ
pub async fn POST(mut req: Request) -> Result<Json<Receipt>> {
    if !req.same_origin() {
        return Err(Error::http(403, "cross-site request refused"));
    }
    // …
}
Source

pub fn is_secure(&self) -> bool

Whether the connection (or the trusted proxy) used HTTPS.

Source

pub async fn bytes(&mut self) -> Result<Bytes, Error>

Read the whole body (enforcing the body limit: 413 when exceeded). Can be called repeatedly; the body is buffered on first read.

Source

pub async fn raw_body(&mut self) -> Result<Bytes, Error>

Raw body — use for webhook signature verification.

Source

pub async fn text(&mut self) -> Result<String, Error>

Source

pub async fn json<T>(&mut self) -> Result<T, Error>

Source

pub async fn form<T>(&mut self) -> Result<T, Error>

application/x-www-form-urlencoded body.

Source

pub fn take_body( &mut self, ) -> Option<UnsyncBoxBody<Bytes, Box<dyn Error + Send + Sync>>>

Take the streaming body (for large uploads). Returns None if the body was already consumed.

Source

pub fn parts(&self) -> &Parts

Trait Implementations§

Source§

impl Debug for Request

Source§

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

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<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

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