pub struct RequestBuilder { /* private fields */ }Expand description
Fluent builder for HttpRequest.
Single-purpose, chainable. Each method returns &mut Self for fluent
chaining; call RequestBuilder::build to obtain the final
HttpRequest.
§Examples
The built request can be sent with HttpRequest::send or
HttpRequest::send_async. This example inspects it without making a network request.
use http_request::{Method, Proxy, RequestBuilder};
let get = RequestBuilder::new()
.get("https://example.com/api")
.header("Accept", "application/json")
.timeout(5_000)
.build();
assert_eq!(get.get_method(), Method::Get);
assert_eq!(get.get_config().get_timeout(), 5_000);
let post = RequestBuilder::new()
.post("https://example.com/api")
.body_json(&serde_json::json!({"key": "value"}))
.header("Content-Type", "application/json")
.proxy(Proxy::https("proxy.example.com", 7890))
.build();
assert_eq!(post.get_method(), Method::Post);
assert_eq!(post.get_body().as_slice(), br#"{"key":"value"}"#);
assert!(post.get_config().get_proxy().is_some());Implementations§
Source§impl RequestBuilder
Inherent implementation of RequestBuilder.
impl RequestBuilder
Inherent implementation of RequestBuilder.
Sourcepub fn remove_header<K: AsRef<str>>(&mut self, key: K) -> &mut Self
pub fn remove_header<K: AsRef<str>>(&mut self, key: K) -> &mut Self
Sourcepub fn clear_headers(&mut self) -> &mut Self
pub fn clear_headers(&mut self) -> &mut Self
Sourcepub fn buffer_size(&mut self, n: usize) -> &mut Self
pub fn buffer_size(&mut self, n: usize) -> &mut Self
Sourcepub fn http1_1_only(&mut self) -> &mut Self
pub fn http1_1_only(&mut self) -> &mut Self
Sourcepub fn http2_only(&mut self) -> &mut Self
pub fn http2_only(&mut self) -> &mut Self
Sourcepub fn redirect(&mut self) -> &mut Self
pub fn redirect(&mut self) -> &mut Self
Enable auto-follow of 3xx redirects.
§Returns
&mut Self- A mutable reference to this builder for chaining.
Sourcepub fn no_redirect(&mut self) -> &mut Self
pub fn no_redirect(&mut self) -> &mut Self
Disable auto-follow of 3xx redirects (default).
§Returns
&mut Self- A mutable reference to this builder for chaining.
Sourcepub fn max_redirect_times(&mut self, n: usize) -> &mut Self
pub fn max_redirect_times(&mut self, n: usize) -> &mut Self
Sourcepub fn decode(&mut self) -> &mut Self
pub fn decode(&mut self) -> &mut Self
Enable automatic response body decompression (gzip / deflate / br).
§Returns
&mut Self- A mutable reference to this builder for chaining.
Sourcepub fn no_decode(&mut self) -> &mut Self
pub fn no_decode(&mut self) -> &mut Self
Disable automatic response body decompression.
§Returns
&mut Self- A mutable reference to this builder for chaining.
Sourcepub fn proxy(&mut self, proxy: Proxy) -> &mut Self
pub fn proxy(&mut self, proxy: Proxy) -> &mut Self
Set the proxy configuration. Pass None (via [Proxy::default()) to
clear an existing proxy.
Construct via Proxy::http / Proxy::https / Proxy::socks5
and optionally chain .auth(user, pass).
§Arguments
Proxy- The proxy configuration to apply to the request.
§Returns
&mut Self- A mutable reference to this builder for chaining.
Sourcepub fn no_proxy(&mut self) -> &mut Self
pub fn no_proxy(&mut self) -> &mut Self
Clear the proxy (use direct connection).
§Returns
&mut Self- A mutable reference to this builder for chaining.
Sourcepub fn build(&mut self) -> HttpRequest
pub fn build(&mut self) -> HttpRequest
Finalise the builder and return the HttpRequest.
§Returns
HttpRequest- The finalised request, leaving the builder empty.