Struct HttpRequest

Source
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

Source

pub fn new() -> HttpRequest

creates a new, empty http request.

§examples
use simplist::HttpRequest;

let request = HttpRequest::new();
Source

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

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

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

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

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

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

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

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

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

pub fn with_body<TBody>(self, body: Option<TBody>) -> HttpRequest
where 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);
Source

pub fn with_header<TKey, TValue>(self, key: TKey, value: TValue) -> HttpRequest
where 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"))

Trait Implementations§

Source§

impl Debug for HttpRequest

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Into<Request> for HttpRequest

Source§

fn into(self) -> Request<Body>

Converts this type into the (usually inferred) input type.

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.