pub struct HttpRequest {
pub params: RouteParams,
pub query: QueryParams,
pub origin_url: Url,
pub method: HttpMethods,
pub path: String,
pub protocol: String,
pub headers: RequestHeaders,
/* private fields */
}Expand description
Represents an incoming HTTP request with comprehensive access to request data.
The HttpRequest struct provides methods to access and manipulate all aspects of an HTTP request including headers, cookies, query parameters, route parameters, and request body content.
§Examples
Basic usage:
use ripress::req::HttpRequest;
let req = HttpRequest::new();
println!("Method: {:?}", req.method);
println!("Path: {}", req.path);
println!("Client IP: {:?}", req.ip());Working with JSON body:
use ripress::context::HttpRequest;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct User {
name: String,
age: u32
}
let req = HttpRequest::new();
if let Ok(user) = req.json::<User>() {
println!("User: {} is {} years old", user.name, user.age);
}Fields§
§params: RouteParamsDynamic route parameters extracted from the URL.
query: QueryParamsQuery parameters from the request URL.
origin_url: UrlThe full URL of the incoming request.
method: HttpMethodsThe HTTP method used for the request (e.g., GET, POST, PUT, DELETE).
path: StringThe requested endpoint path.
protocol: StringProtocol of the request (HTTP or HTTPs)
headers: RequestHeadersThe request’s headers
Implementations§
Source§impl HttpRequest
impl HttpRequest
Retrieves a cookie value by name.
§Arguments
name- The name of the cookie to retrieve
§Returns
Returns Some(&String) with the cookie value if found, or None if not found.
§Example
use ripress::context::HttpRequest;
let req = HttpRequest::new();
match req.get_cookie("session_id") {
Some(session) => println!("Session ID: {}", session),
None => println!("No session cookie found")
}Sourcepub fn get_all_data(&self) -> &RequestData
pub fn get_all_data(&self) -> &RequestData
Sourcepub fn is(&self, content_type: RequestBodyType) -> bool
pub fn is(&self, content_type: RequestBodyType) -> bool
Checks if the request body matches a specific content type.
§Arguments
content_type- TheRequestBodyTypeto check against
§Returns
Returns true if the content type matches, false otherwise.
§Example
use ripress::req::HttpRequest;
use ripress::req::body::RequestBodyType;
let req = HttpRequest::new();
if req.is(RequestBodyType::JSON) {
// Handle JSON content
}Sourcepub fn bytes(&self) -> Result<&[u8], String>
pub fn bytes(&self) -> Result<&[u8], String>
Returns a read-only view of the raw request body when it is binary.
Returns:
Ok(&[u8])whencontent_typeisRequestBodyType::BINARY.Err("Invalid Binary Content")if the internal variant does not match the declared type.Err(...)describing the actual content type if it is not binary.
§Example
let req = ripress::context::HttpRequest::new();
if let Ok(bytes) = req.bytes() {
// process bytes...
}Sourcepub fn json<J>(&self) -> Result<J, String>where
J: DeserializeOwned + Serialize,
pub fn json<J>(&self) -> Result<J, String>where
J: DeserializeOwned + Serialize,
Deserializes the request body as JSON into the specified type.
§Type Parameters
J- The type to deserialize into, must implementDeserializeOwned
§Returns
Returns Ok(J) with the deserialized value if successful, or
Err(String) with an error message if deserialization fails.
§Example
use ripress::context::HttpRequest;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct LoginData {
username: String,
password: String
}
let req = HttpRequest::new();
match req.json::<LoginData>() {
Ok(data) => println!("Login attempt for: {}", data.username),
Err(e) => println!("Invalid login data: {}", e)
}Sourcepub fn text(&self) -> Result<&str, String>
pub fn text(&self) -> Result<&str, String>
Returns request’s text body.
§Example
let req = ripress::req::HttpRequest::new();
let text = req.text().unwrap();
println!("text : {:?}", text);This function returns the text body of the request.
Returns an Result<String>, where Ok(String) contains the body if it is valid text, or Err(error) if it is not.
Sourcepub fn form_data(&self) -> Result<&FormData, String>
pub fn form_data(&self) -> Result<&FormData, String>
Returns request’s form_data body.
§Example
let req = ripress::req::HttpRequest::new();
// Let' say form data was sent as key=value and key2=value2
let form_data = req.form_data().unwrap();
println!("key = : {:?}", form_data.get("key"));
println!("key2 = : {:?}", form_data.get("key2"));This function returns a HashMap of the form data.
Returns an Result<HashMap<String, String>>, where Ok(HashMap<String, String>) contains the form_data if it is valid form data, or Err(error) if it is not.
Sourcepub fn insert_form_field(&mut self, key: &str, value: &str)
pub fn insert_form_field(&mut self, key: &str, value: &str)
Inserts a key-value pair into the request’s form data.
If the current body is not FORM, this will initialize an empty FormData
and set the body’s content type to FORM before inserting the field.
This is useful for middlewares that wish to expose computed values through
the form_data() API, such as attaching file upload metadata.
Trait Implementations§
Source§impl Clone for HttpRequest
impl Clone for HttpRequest
Source§fn clone(&self) -> HttpRequest
fn clone(&self) -> HttpRequest
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HttpRequest
impl Debug for HttpRequest
Source§impl Default for HttpRequest
impl Default for HttpRequest
Source§impl ExtractFromOwned for HttpRequest
impl ExtractFromOwned for HttpRequest
Source§type Error = Infallible
type Error = Infallible
Source§fn extract_from_owned(req: HttpRequest) -> Result<Self, Self::Error>
fn extract_from_owned(req: HttpRequest) -> Result<Self, Self::Error>
HttpRequest. Read more