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
Method | Return Type | Description |
---|---|---|
get_body() | RequestBody | Returns 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() | &HttpMethod | Returns 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() | &str | Returns the request path. |
get_peer_addr() | &SocketAddr | Returns 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
impl Request
Sourcepub fn builder() -> RequestBuilder
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
impl Request
Sourcepub fn get_headers(&self) -> &HashMap<String, String>
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");
}
Sourcepub fn get_header(&self, key: &str) -> Option<&str>
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");
}
Sourcepub fn get_body(&self) -> &RequestBody
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
}
}
}
Sourcepub fn get_peer_addr(&self) -> &SocketAddr
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();
}
Sourcepub fn get_query_params(&self) -> &HashMap<String, String>
pub fn get_query_params(&self) -> &HashMap<String, String>
Returns the queries of the HTTP request as a HashMap
The path of the HTTP request | Value |
---|---|
/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>>();
}
Sourcepub fn get_query_param(&self, key: &str) -> Option<&String>
pub fn get_query_param(&self, key: &str) -> Option<&String>
Returns the query parameter of the HTTP request
The path of the HTTP request | get_query_param(key: &str) | Returns |
---|---|---|
/hello?planet=earth | get_query_param("planet") | Some(“earth”) |
/hello?planet=earth | get_query_param("moon") | None |
/hello?planet=earth&moon=luna | get_query_param("moon") | Some(“luna”) |
§Example
use krustie::{ Request, Response };
fn get(request: &Request, response: &mut Response) {
let id = request.get_query_param("id");
}
Sourcepub fn get_path_array(&self) -> &Vec<String>
pub fn get_path_array(&self) -> &Vec<String>
Returns the path of the HTTP request as a Vector
The path of the HTTP request | get_path_array() |
---|---|
/ | vec![] |
/hello | vec!["hello"] |
/hello/world | vec!["hello", "world"] |
/hello/world?city=istanbul | vec!["hello", "world?city=istanbul"] |
§Example
use krustie::{ Request, Response };
fn get(request: &Request, response: &mut Response) {
let path: &Vec<String> = request.get_path_array();
}
Sourcepub fn get_path(&self) -> &str
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();
}
Sourcepub fn get_param(&self, key: &str) -> Option<&String>
pub fn get_param(&self, key: &str) -> Option<&String>
Returns the requested parameter of the HTTP request
Route | Path | get_param(key: &str) | Returns |
---|---|---|---|
/hello/:name | /hello/marvin | get_param("name") | Some("marvin") |
/hello/:name | /hello/marvin | get_param("planet") | None |
/hello/:name/:planet | /hello/marvin/earth | get_param("planet") | Some("earth") |
§Example
use krustie::{ Request, Response };
fn get(request: &Request, response: &mut Response) {
let sort: Option<&String> = request.get_param("sort");
}
Sourcepub fn get_version(&self) -> &str
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");
}
Sourcepub fn get_method(&self) -> &HttpMethod
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);
}