Request

Struct Request 

Source
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: RequestType

The type of request to be performed (GET, HEAD, DELETE, POST, etc…)

§url_string: String

The 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: String

The domain of the server to connect to

§path: String

The path to target our requests at

§protocol: HTTPVersion

The 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: usize

the number of headers this request stores in headers

Implementations§

Source§

impl Request

Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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(())
}
Source

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

Source

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.

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<'_>) -> Result

Formats the value using the given formatter. 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)
Performs copy-assignment from self to dest. Read more
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.