pub struct Request {
pub request_type: RequestType,
pub url_string: String,
pub domain: String,
pub path: String,
pub protocol: HTTPVersion,
pub body: Option<(String, String)>,
pub headers: HashMap<String, String>,
pub header_count: usize,
}Expand description
Build a request, without any of the knowledge of how HTTP works, this structure takes care of it all, and still follows specifications
Fields§
§request_type: RequestTypeThe type of request to be performed (GET, HEAD, DELETE, POST, etc…)
url_string: StringThe raw URL input when you initialise the request, this is stored for you, we dont actually need to store this as we dont use it :)
domain: StringThe domain of the server to connect to
path: StringThe path to target our requests at
protocol: HTTPVersionThe protocol to use: This can be HTTP, or HTTPS If a server requests we use HTTPS, we will automatically switch over anyway, no fiddling needed
body: Option<(String, String)>Not all requests have a body, this is an optional field containing a tuple value of both the encoding, and the body content
headers: HashMap<String, String>This stores the values of each header you set within the request. This is the first step to authenticating a request
header_count: usizethe number of headers this request stores in headers
Implementations§
Source§impl Request
impl Request
Sourcepub fn get<A: Into<String>>(url: A) -> Request
pub fn get<A: Into<String>>(url: A) -> Request
This method is used to GET content from a url:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let response = Request::get("https://example.com//path/to/resource")
.send()?;
println!("{:#?}", response);
Ok(())
}Sourcepub fn head<A: Into<String>>(url: A) -> Request
pub fn head<A: Into<String>>(url: A) -> Request
This method is used to read the HEAD content from a url. It is often used for checking the content-length before sending a GET request, but in our case it is open to you to use:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let response = Request::head("https://example.com//path/to/resource")
.send()?;
println!("{:#?}", response);
Ok(())
}Sourcepub fn delete<A: Into<String>>(url: A) -> Request
pub fn delete<A: Into<String>>(url: A) -> Request
This method is used to DELETE content from a url. It is used to inform the server that the content at the requested path is to be removed
fn main() -> Result<(), Box<dyn std::error::Error>> {
let response = Request::delete("https://example.com//path/to/resource")
.send()?;
println!("{:#?}", response);
Ok(())
}Sourcepub fn options<A: Into<String>>(url: A) -> Request
pub fn options<A: Into<String>>(url: A) -> Request
This method is used to check what can be done at the URL provided It is method is primarily used internally for confirming CORS protocol before allowing a request to send
fn main() -> Result<(), Box<dyn std::error::Error>> {
let response = Request::options("https://example.com//path/to/resource")
.send()?;
println!("{:#?}", response);
Ok(())
}Sourcepub fn post<A: Into<String>>(url: A) -> Request
pub fn post<A: Into<String>>(url: A) -> Request
This method is used for transmitting data to the server through the request body.
this example outputs the data as application/x-www-form-urlencoded and uses a tuple for key-value input:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let post_body: Vec<(&str, &str)> = vec!(
("author", "Altrius"),
("timestamp", "Fri, 28 Aug 2020 10:55:44 +0000")
);
let post_data = PostData::from_tuple(post_body);
let response = Request::post("https://example.com//documents")
.set_body(&post_data)
.send()?;
println!("{:#?}", response);
Ok(())
}Sourcepub fn set_body(&mut self, body: &PostData) -> &mut Request
pub fn set_body(&mut self, body: &PostData) -> &mut Request
This method is used to set the body of a request.
It takes one parameter only, and that is a PostData structure.
This result of this method is only used in a POST request, it is not necessary for any other request type.
see the example of a POST request for usage
Sourcepub fn send(&self) -> Result<Response, Box<dyn Error>>
pub fn send(&self) -> Result<Response, Box<dyn Error>>
The send method is used to deserialize and send the resulting request to the destination, it uses a series of checks to confirm that it is doing what you want it to do
see any of the above examples for information on how to use this method.