Struct flawless_http::Request

source ·
pub struct Request<T>
where T: HTTPTransport,
{ /* private fields */ }
Expand description

An HTTP request.

Implementations§

source§

impl<T: HTTPTransport> Request<T>

source

pub fn new<M, U>(transport: T, method: M, url: U) -> Request<T>
where M: Into<String>, U: Into<String>,

Create a new request over a provided transport layer.

source

pub fn send(self) -> Result<Response, Error>

Perform the request.

source

pub fn timeout(self, duration: Duration) -> Self

Default: 120s

Timeout errors are of kind ErrorKind::Io. Call Error::message() for more information.

use std::time::Duration;
use flawless_http::{get, ErrorKind};

let response = get("https://httpbin.org/delay/2")
                 .timeout(Duration::from_secs(1))
                 .send();
assert!(response.is_err());
assert_eq!(response.err().unwrap().kind(), ErrorKind::Io);
source

pub fn response_body_limit(self, limit: u64) -> Self

Sets the size limit of a response body.

Default: 10_000_000 (~10Mb)

In case a response returns a bigger body, it will be truncated.

use flawless_http::get;

let response = get("https://httpbin.org/get")
                 .response_body_limit(100)
                 .send();
assert!(response.is_ok());
assert_eq!(response.unwrap().text().unwrap().len(), 100);
source

pub fn header<N: AsRef<str>>(&self, name: N) -> Option<&String>

Gets header by name if it exists, otherwise returns None.

If multiple headers exist with the same name, will return the first one. HTTP header names are NOT case-sensitive and case will be ignored during lookup.

source

pub fn set_header<N, V>(self, name: N, value: V) -> Self
where N: Into<String>, V: Into<String>,

Sets a request header.

If the header already exists, it will be overwritten. HTTP header names are NOT case-sensitive and case will be ignored during comparison. dsfsd sdfsdffsdf sdfsd

use serde_json::Value;
use flawless_http::get;

let response = get("https://httpbin.org/get")
                 .set_header("Accept", "text/plain")
                 .send();
let json: Value = response.unwrap().json().unwrap();
let accept_header = json["headers"]["Accept"].as_str();
assert_eq!(accept_header, Some("text/plain"));
source

pub fn body<B: IntoBody<T>>(self, body: B) -> Self

Sets the body of a request.

Depending on the argument, additional request headers might be set. If headers are already manually set by the user, this call should not override them.

§Text arguments

In case a String or &str type is given to body, the "Content-Length" header is going to be set to the byte length of the string.

use flawless_http::post;

let response = post("https://httpbin.org/post")
                 .body("Hello world!")
                 .send();
assert!(response.is_ok());
§Form arguments

In case a slice of tuples of strings is passed in (&[(&str, &str)]), body will assume a for is being submitted and URL encode it. It will set the header "Content-Type" to "application/x-www-form-urlencoded", and "Content-Length" to the size of the encoded content.

use flawless_http::post;

let response = post("https://httpbin.org/post")
                 .body([
                    ("Hello", "world!"),
                    ("second", "argument"),
                  ].as_ref())
                 .send();
assert!(response.is_ok());
§JSON arguments

In case of a serde_json::Value type, body will assume that the content is of type JSON and set the header "Content-Type" to "application/json". It will also set "Content-Length" to the size of the serialized JSON.

use flawless_http::post;
use serde_json::json;

let response = post("https://httpbin.org/post")
                 .body(json!({
                    "Hello": "world!",
                    "second": "argument",
                  }))
                 .send();
assert!(response.is_ok());

Trait Implementations§

source§

impl<T> Clone for Request<T>
where T: HTTPTransport + Clone,

source§

fn clone(&self) -> Request<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Request<T>
where T: HTTPTransport + Debug,

source§

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

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

impl Idempotence for Request<Flawless>

source§

fn idempotent(self) -> Self::Idempotent

Mark the request as idempotent.

Idempotent requests will automatically be retried in case of timeouts or errors.

§

type Idempotent = Request<Idempotent>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Request<T>
where T: RefUnwindSafe,

§

impl<T> Send for Request<T>
where T: Send,

§

impl<T> Sync for Request<T>
where T: Sync,

§

impl<T> Unpin for Request<T>
where T: Unpin,

§

impl<T> UnwindSafe for Request<T>
where T: UnwindSafe,

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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.