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
impl Request
Sourcepub fn new<A: ToUrl>(
url: A,
headers: Option<Vec<Header<'_>>>,
body: Option<String>,
) -> Result<Self, Box<dyn Error>>
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));
Sourcepub fn default_get_request<A: ToUrl>(url: A) -> Result<Self, Box<dyn Error>>
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");
Sourcepub fn execute(&self) -> Result<Response, NanoGetError>
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);
Sourcepub fn get_request_headers(&self) -> impl Iterator<Item = (&str, &str)>
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);
}
Sourcepub fn is_https(&self) -> bool
pub fn is_https(&self) -> bool
Convenience method to check if the request is a https request based on the embedded url’s protocol.
Sourcepub fn get_request_type(&self) -> &str
pub fn get_request_type(&self) -> &str
Returns the type of HTTP Request.
Currently only returns "GET"
. For Future Use.
Sourcepub fn add_header(&mut self, key: &str, value: &str)
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.