pub struct HttpRequest { /* private fields */ }
Expand description
a http request.
this struct allows you to customize specific details in a http request, and let you use a non-standard http method, set http headers, or read and manipulate the url bit-by-bit.
in general, using the provided fn get()
, fn post()
, fn delete()
in HttpClient
will be much simpler.
§examples
use simplist::HttpClient;
use simplist::HttpMethod;
use simplist::HttpRequest;
let client = HttpClient::new(...);
let custom = HttpRequest::new()
.with_url("https://hinaria.com".parse()?)
.with_method(HttpMethod::Get);
let response = await client.request(custom)?;
let string = response.read_as_string()?;
println!("{:?}", string);
// => Ok("<!doctype html>\n<html lang=\"en\">\n<head>\n <meta charset=\"utf-8\"> ...")
Implementations§
Source§impl HttpRequest
impl HttpRequest
Sourcepub fn new() -> HttpRequest
pub fn new() -> HttpRequest
creates a new, empty http request.
§examples
use simplist::HttpRequest;
let request = HttpRequest::new();
Sourcepub fn with(url: Url, method: HttpMethod) -> HttpRequest
pub fn with(url: Url, method: HttpMethod) -> HttpRequest
creates a new http request for the specified url
and method
.
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
let url = "https://hinaria.com".parse()?;
let method = HttpMethod::Get;
let request = HttpRequest::with(url, method);
Sourcepub fn url(&self) -> &Url
pub fn url(&self) -> &Url
returns the url for this request.
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
let url = "https://hinaria.com".parse()?;
let request = HttpRequest::with(url.clone(), HttpMethod::Get);
assert_eq!(request.url(), url);
Sourcepub fn method(&self) -> HttpMethod
pub fn method(&self) -> HttpMethod
returns the method for this request.
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
let request = HttpRequest::with("https://hinaria.com", HttpMethod::Get);
assert_eq!(request.method(), HttpMethod::Get);
Sourcepub fn path(&self) -> &str
pub fn path(&self) -> &str
returns the url path for this request.
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
let url = "http://hinaria.com/hello/index.html?q=search".parse()?;
let request = HttpRequest::with(url, HttpMethod::Get);
assert_eq!(request.path(), "/hello/index.html");
Sourcepub fn query(&self) -> Option<&str>
pub fn query(&self) -> Option<&str>
returns the url query for this request.
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
let url = "http://hinaria.com/hello/index.html?q=search".parse()?;
let request = HttpRequest::with(url, HttpMethod::Get);
assert_eq!(request.query(), Some("q=search"));
Sourcepub fn headers(&self) -> &Headers
pub fn headers(&self) -> &Headers
returns the headers for this request.
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
let request = HttpRequest::with("https://hinaria.com", HttpMethod::Get);
let headers = request.headers();
assert_eq!(headers.has::<ContentType>(), true);
Sourcepub fn headers_mut(&mut self) -> &mut Headers
pub fn headers_mut(&mut self) -> &mut Headers
returns a mutable reference to the headers for this request.
§examples
use hyper::headers::Basic;
use simplist::HttpMethod;
use simplist::HttpRequest;
let request = HttpRequest::with("https://hinaria.com", HttpMethod::Get);
let headers = request.headers_mut();
headers.set(Basic {
username: "annie".to_owned(),
password: None,
});
Sourcepub fn with_method(self, method: HttpMethod) -> HttpRequest
pub fn with_method(self, method: HttpMethod) -> HttpRequest
sets this request’s method to method
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
HttpRequest::new()
.with_url ("https://hinaria.com".parse()?)
.with_method(HttpMethod::Get);
Sourcepub fn with_url(self, url: Url) -> HttpRequest
pub fn with_url(self, url: Url) -> HttpRequest
sets this request’s url to url
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
HttpRequest::new()
.with_url ("https://hinaria.com".parse()?)
.with_method(HttpMethod::Get);
Sourcepub fn with_body<TBody>(self, body: Option<TBody>) -> HttpRequestwhere
TBody: Into<HttpContent>,
pub fn with_body<TBody>(self, body: Option<TBody>) -> HttpRequestwhere
TBody: Into<HttpContent>,
attaches body
to this request.
§examples
§sending a json payload.
use hyper::header::ContentType;
use simplist::HttpMethod;
use simplist::HttpRequest;
HttpRequest::new()
.with_url ("https://api.hinaria.com/users/@me/database".parse()?)
.with_method(HttpMethod::Put)
.with_header(ContentType::json())
.with_body ("{ id: 1234 }");
§sending an octet stream.
use hyper::header::ContentType;
use simplist::HttpMethod;
use simplist::HttpRequest;
let data: Vec<u8> = ...;
HttpRequest::new()
.with_url ("https://api.hinaria.com/users/@me/database".parse()?)
.with_method(HttpMethod::Put)
.with_header(ContentType::octet_stream())
.with_body (data);
Sourcepub fn with_header<TKey, TValue>(self, key: TKey, value: TValue) -> HttpRequest
pub fn with_header<TKey, TValue>(self, key: TKey, value: TValue) -> HttpRequest
sets the header key
to value
in this request.
body
can be any object which can be converted to a HttpContent
. for more information, see HttpContent
.
§examples
use simplist::HttpMethod;
use simplist::HttpRequest;
let request = HttpRequest::with("https://hinaria.com", HttpMethod::Get)
.with_header(Basic { username: "annie".to_owned(), password: None });
.with_header(ContentType::json())
.with_header(Referer::new("mozilla.org"))