Struct simplist::HttpRequest
[−]
[src]
pub struct HttpRequest { /* fields omitted */ }
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\"> ...")
Methods
impl HttpRequest
[src]
fn new() -> HttpRequest
[src]
creates a new, empty http request.
examples
use simplist::HttpRequest; let request = HttpRequest::new();
fn with(url: Url, method: HttpMethod) -> HttpRequest
[src]
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);
fn url(&self) -> &Url
[src]
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);
fn method(&self) -> HttpMethod
[src]
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);
fn path(&self) -> &str
[src]
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");
fn query(&self) -> Option<&str>
[src]
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"));
fn headers(&self) -> &Headers
[src]
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);
fn headers_mut(&mut self) -> &mut Headers
[src]
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, });
fn with_method(self, method: HttpMethod) -> HttpRequest
[src]
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);
fn with_url(self, url: Url) -> HttpRequest
[src]
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);
fn with_body<TBody>(self, body: Option<TBody>) -> HttpRequest where
TBody: Into<HttpContent>,
[src]
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);
fn with_header<TKey, TValue>(self, key: TKey, value: TValue) -> HttpRequest where
TKey: Into<Cow<'static, str>>,
TValue: Into<Raw>,
[src]
TKey: Into<Cow<'static, str>>,
TValue: Into<Raw>,
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"))