Request

Struct Request 

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

Represents the incoming HTTP request

It contains information about the request, such as the request method, path, query string, parameters, headers, and body.

Request cannot be accessed directly or modified anywhere in the code. It can only be accessed through the handler or middleware function with an immutable reference via methods.

§All Methods

MethodReturn TypeDescription
get_body()RequestBodyReturns the body of the request.
get_header(key: &str)Option<&str>Returns the value of the specified header.
get_headers()&HashMap<String, String>Returns a reference to the request headers.
get_method()&HttpMethodReturns the request method.
get_param(key: &str)Option<&str>Returns the value of the parameter with the specified name.
get_params()&HashMap<String, String>Returns a map of all parameters.
get_path_array()Vec<String>Returns the request path as an array of segments.
get_path()&strReturns the request path.
get_peer_addr()&SocketAddrReturns the address of the peer that sent the request.
get_query_param(key: &str)Option<&str>Returns the value of the query parameter with the specified name.
get_query_params()HashMap<String, String>Returns a map of all query parameters.

Implementations§

Source§

impl Request

Source

pub fn builder() -> RequestBuilder

Returns a new RequestBuilder instance

§Example
use krustie::{Request, HttpMethod};

let request = Request::builder()
  .method(HttpMethod::GET)
  .build();

assert_eq!(request.get_method(), &HttpMethod::GET);
Source§

impl Request

Source

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

Returns the reference of the Request headers as a HashMap

§Example
use krustie::{ request::Request, response::Response};

fn get(request: &Request, response: &mut Response) {
  let headers = request.get_headers();
  let content_type = headers.get("content-type");
}
Source

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

Returns the value of the requested header

§Example
use krustie::{ request::Request, response::Response};

fn get(request: &Request, response: &mut Response) {
  let content_type = request.get_header("content-type");
}
Source

pub fn get_body(&self) -> &RequestBody

Returns the body of the HTTP request

The body can be of type Text, Json, Form or None

§Example
use krustie::{ Request, Response, request::RequestBody };

fn get(request: &Request, response: &mut Response) {
  match request.get_body() {
    RequestBody::Text(body) => {
      // Do something with the body
    },
    RequestBody::Json(json) => {
     // Do something with the json
    },
    _ => {
     // Do something else
    }
  }
}
Source

pub fn get_peer_addr(&self) -> &SocketAddr

Returns the peer address of the HTTP request

The peer address is the ip address of the client that made the request

§Example
use krustie::{ Request, Response };
use std::net::SocketAddr;

fn get(request: &Request, response: &mut Response) {
  let peer_addr: &SocketAddr = request.get_peer_addr();
}
Source

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

Returns the queries of the HTTP request as a HashMap

The path of the HTTP requestValue
/hello[]
/hello?planet=earth[{ "planet": "earth" }]
/hello?planet=earth&moon=luna[{ "planet": "earth" }, { "moon": "luna"}]
§Example
use krustie::{ Request, Response };

fn get(request: &Request, response: &mut Response) {
  let queries = request
    .get_query_params()
    .iter()
    .map(|(k, v)| format!("{}: {}", k, v))
    .collect::<Vec<String>>();
}
Source

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

Returns the query parameter of the HTTP request

The path of the HTTP requestget_query_param(key: &str)Returns
/hello?planet=earthget_query_param("planet")Some(“earth”)
/hello?planet=earthget_query_param("moon")None
/hello?planet=earth&moon=lunaget_query_param("moon")Some(“luna”)
§Example
use krustie::{ Request, Response };

fn get(request: &Request, response: &mut Response) {
  let id = request.get_query_param("id");
}
Source

pub fn get_path_array(&self) -> &Vec<String>

Returns the path of the HTTP request as a Vector

The path of the HTTP requestget_path_array()
/vec![]
/hellovec!["hello"]
/hello/worldvec!["hello", "world"]
/hello/world?city=istanbulvec!["hello", "world?city=istanbul"]
§Example
use krustie::{ Request, Response };

fn get(request: &Request, response: &mut Response) {
  let path: &Vec<String> = request.get_path_array();
}
Source

pub fn get_path(&self) -> &str

Returns the path of the HTTP request as a String

§Example
use krustie::{ Request, Response };

fn get(request: &Request, response: &mut Response) {
  let path: &str = request.get_path();
}
Source

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

Returns the requested parameter of the HTTP request

RoutePathget_param(key: &str)Returns
/hello/:name/hello/marvinget_param("name")Some("marvin")
/hello/:name/hello/marvinget_param("planet")None
/hello/:name/:planet/hello/marvin/earthget_param("planet")Some("earth")
§Example
use krustie::{ Request, Response };

fn get(request: &Request, response: &mut Response) {
  let sort: Option<&String> = request.get_param("sort");
}
Source

pub fn get_version(&self) -> &str

Returns the version of the HTTP request

§Example
use krustie::{HttpMethod, Request, Response};

fn get(request: &Request, response: &mut Response) {
    assert_eq!(request.get_version(), "HTTP/1.1");
}
Source

pub fn get_method(&self) -> &HttpMethod

Returns the method of the HTTP request

§Example
use krustie::{HttpMethod, Request, Response};

fn get(request: &Request, response: &mut Response) {
   assert_eq!(request.get_method(), &HttpMethod::GET);
}

Trait Implementations§

Source§

impl Clone for Request

Source§

fn clone(&self) -> Request

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Request

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Request

Source§

fn default() -> Self

Returns the “default value” for a type. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.