[][src]Struct nano_get::Request

pub struct Request {
    pub url: Url,
    pub body: Option<String>,
    // some fields omitted
}

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.

Methods

impl Request[src]

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

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));

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

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");

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

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);

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

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);
}

pub fn is_https(&self) -> bool[src]

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

pub fn get_request_type(&self) -> &str[src]

Returns the type of HTTP Request.

Currently only returns "GET". For Future Use.

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

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

impl Debug for Request[src]

Auto Trait Implementations

impl RefUnwindSafe for Request

impl Send for Request

impl Sync for Request

impl Unpin for Request

impl UnwindSafe for Request

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.