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>
impl<T: HTTPTransport> Request<T>
sourcepub fn new<M, U>(transport: T, method: M, url: U) -> Request<T>
pub fn new<M, U>(transport: T, method: M, url: U) -> Request<T>
Create a new request over a provided transport layer.
sourcepub fn timeout(self, duration: Duration) -> Self
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);
sourcepub fn response_body_limit(self, limit: u64) -> Self
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);
sourcepub fn header<N: AsRef<str>>(&self, name: N) -> Option<&String>
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.
sourcepub fn set_header<N, V>(self, name: N, value: V) -> Self
pub fn set_header<N, V>(self, name: N, value: V) -> Self
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"));
sourcepub fn body<B: IntoBody<T>>(self, body: B) -> Self
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 Idempotence for Request<Flawless>
impl Idempotence for Request<Flawless>
source§fn idempotent(self) -> Self::Idempotent
fn idempotent(self) -> Self::Idempotent
Mark the request as idempotent.
Idempotent requests will automatically be retried in case of timeouts or errors.