pub struct Request { /* private fields */ }Expand description
An HTTP request as seen by middleware, API routes and server actions.
Implementations§
Source§impl Request
impl Request
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
Sourcepub fn from_http(req: Request<Bytes>) -> Request
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).
pub fn method(&self) -> &Method
pub fn uri(&self) -> &Uri
pub fn path(&self) -> &str
pub fn query_string(&self) -> &str
Sourcepub fn query<T>(&self) -> Result<T, Error>where
T: DeserializeOwned,
pub fn query<T>(&self) -> Result<T, Error>where
T: DeserializeOwned,
Deserialize the query string into T.
Sourcepub fn query_param(&self, name: &str) -> Option<String>
pub fn query_param(&self, name: &str) -> Option<String>
A single query parameter.
pub fn headers(&self) -> &HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
Header value as text (None if missing or not visible ASCII).
pub fn params(&self) -> &Params
pub fn param(&self, name: &str) -> Option<&str>
pub fn remote_addr(&self) -> Option<SocketAddr>
Sourcepub fn client_ip(&self) -> Option<IpAddr>
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.
pub fn extensions(&self) -> &Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Sourcepub fn extension<T>(&self) -> Option<&T>
pub fn extension<T>(&self) -> Option<&T>
Typed request-scoped value inserted by middleware (e.g. the current user).
pub fn insert_extension<T>(&mut self, value: T)
Sourcepub fn set_path(&mut self, path_and_query: &str) -> Result<(), Error>
pub fn set_path(&mut self, path_and_query: &str) -> Result<(), Error>
Rewrite the request path (middleware only; routing happens afterwards).
Sourcepub fn set_body_limit(&mut self, limit: usize)
pub fn set_body_limit(&mut self, limit: usize)
Override the maximum body size for this request.
pub fn is_websocket_upgrade(&self) -> bool
Sourcepub fn same_origin(&self) -> bool
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"));
}
// …
}Sourcepub async fn bytes(&mut self) -> Result<Bytes, Error>
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.
Sourcepub async fn raw_body(&mut self) -> Result<Bytes, Error>
pub async fn raw_body(&mut self) -> Result<Bytes, Error>
Raw body — use for webhook signature verification.
pub async fn text(&mut self) -> Result<String, Error>
pub async fn json<T>(&mut self) -> Result<T, Error>where
T: DeserializeOwned,
Sourcepub async fn form<T>(&mut self) -> Result<T, Error>where
T: DeserializeOwned,
pub async fn form<T>(&mut self) -> Result<T, Error>where
T: DeserializeOwned,
application/x-www-form-urlencoded body.
Sourcepub fn take_body(
&mut self,
) -> Option<UnsyncBoxBody<Bytes, Box<dyn Error + Send + Sync>>>
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.