Struct Request

Source
pub struct Request {
    pub url: Url,
    pub body: Option<String>,
    /* private fields */
}
Expand description

This is the basic HTTP Request Object.

This is self-containing and you can execute the request using its execute method. It invokes a http or https version depending on the protocol of the embedded url and based on the "https" feature flag.

The major difference between this and the usual get method in the crate is the more fine grained control you get in the request and response.

Running the HTTP(s) method is as simple as calling Request.execute() on the constructed request. This returns a Response object instead of the body of the response as a String.

§Request Body

Although, the standard doesn’t recommend sending a body with a get request, you can provide an optional body for the request.

§Example

let mut request = nano_get::Request::default_get_request("http://example.com/").unwrap();
request.body = Some("Hello World!".to_string());

§Additional Request Headers

You can provide additional headers as part of your request by using the add_header(key: &str, value: &str) method. These will be sent along with the default headers as part of the request.

§Example

let mut request = nano_get::Request::default_get_request("http://example.com/").unwrap();
request.add_header("test", "value testing");

§Executing the Request

As mentioned earlier, executing the request is as simple as calling Request.execute().

This is similar to the basic unified HTTP GET in this crate nano_get::get(), in the way it handles http/https.

If the protocol of the embedded url is https and if the "https" feature flag is present, the https version of get, based on the openssl crate is executed.

The fall-back is the regular HTTP GET.

§Example

For regular HTTP GET requests,

use nano_get::Response;
let mut request = nano_get::Request::default_get_request("http://example.com/").unwrap();
request.add_header("test", "value testing");
let response: Response = request.execute().unwrap();

Fields§

§url: Url

The embedded Url that is part of the request. This is used while executing the HTTP Request.

§body: Option<String>

The optional body of the request, that is sent while executing the request.

Implementations§

Source§

impl Request

Source

pub fn new<A: ToUrl>( url: A, headers: Option<Vec<Header<'_>>>, body: Option<String>, ) -> Result<Self, Box<dyn Error>>

Creates a new Request object, based on the url, and optional headers.

§Examples
use nano_get::Request;
let request = Request::new("http://example.com", None, None);

To include custom headers,

use nano_get::Request;
let request_headers = vec![("header1", "value1"), ("header2", "value2")];
let request = Request::new("http://example.com", Some(request_headers), None);

To include custom headers and body

use nano_get::Request;
let request_headers = vec![("header1", "value1"), ("header2", "value2")];
let request_body = "Hello World!!".to_string();
let request = Request::new("http://example.com", Some(request_headers), Some(request_body));
Source

pub fn default_get_request<A: ToUrl>(url: A) -> Result<Self, Box<dyn Error>>

Simplified version to create a Request based only on the given Url.

Default Headers are inserted and the Body is set to None. The values can be modified if required later.

§Example
use nano_get::Request;
let request = Request::default_get_request("http://example.com");
Source

pub fn execute(&self) -> Result<Response, NanoGetError>

Executes the request and returns a nano_get::Response object based std::result::Result.

If the protocol of the embedded url is https and if the "https" feature flag is present, the https version of get, based on the openssl crate is executed.

§Example
use nano_get::Response;

let mut request = nano_get::Request::default_get_request("http://example.com/").unwrap();
request.add_header("test", "value testing");
let response: Response = request.execute().unwrap();
println!("{}", response.status);
println!("{}", response.body);
Source

pub fn get_request_headers(&self) -> impl Iterator<Item = (&str, &str)>

Returns the headers as an Iterator over the key-value pairs.

§Example
use nano_get::Response;

let mut request = nano_get::Request::default_get_request("http://example.com/").unwrap();
request.add_header("test", "value testing");
for (k, v) in request.get_request_headers() {
    println!("{}, {}", k, v);
}
Source

pub fn is_https(&self) -> bool

Convenience method to check if the request is a https request based on the embedded url’s protocol.

Source

pub fn get_request_type(&self) -> &str

Returns the type of HTTP Request.

Currently only returns "GET". For Future Use.

Source

pub fn add_header(&mut self, key: &str, value: &str)

Add an additional header to the request.

You can overwrite existing values by adding the header with the new value.

You cannot however remove the presence of a header.

Trait Implementations§

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> 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, 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.