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
use http_request::{RequestBuilder, Proxy};
// sync GET
let mut req = RequestBuilder::new()
.get("https://example.com/api")
.header("Accept", "application/json")
.timeout(5_000)
.build();
let _resp = req.send();
// async POST with JSON body
let req = RequestBuilder::new()
.post("https://example.com/api")
.body_json(&serde_json::json!({"k": "v"}))
.header("Content-Type", "application/json")
.proxy(Proxy::https("127.0.0.1", 7890).auth("u", "p"))
.build();
let _resp = req.send_async().await;Implementations§
Source§impl RequestBuilder
impl RequestBuilder
Sourcepub fn get(&mut self, url: impl Into<String>) -> &mut Self
pub fn get(&mut self, url: impl Into<String>) -> &mut Self
Shortcut for method(Method::Get) + url(url).
Sourcepub fn post(&mut self, url: impl Into<String>) -> &mut Self
pub fn post(&mut self, url: impl Into<String>) -> &mut Self
Shortcut for method(Method::Post) + url(url).
Sourcepub fn method(&mut self, method: Method) -> &mut Self
pub fn method(&mut self, method: Method) -> &mut Self
Set HTTP method explicitly (Method::Get / Method::Post / etc.).
Sourcepub fn header<K: AsRef<str>, V: AsRef<str>>(
&mut self,
key: K,
value: V,
) -> &mut Self
pub fn header<K: AsRef<str>, V: AsRef<str>>( &mut self, key: K, value: V, ) -> &mut Self
Set a single header (last write wins on duplicate keys).
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
Remove a header by key.
Sourcepub fn clear_headers(&mut self) -> &mut Self
pub fn clear_headers(&mut self) -> &mut Self
Clear all headers.
Sourcepub fn body_text<T: Into<String>>(&mut self, text: T) -> &mut Self
pub fn body_text<T: Into<String>>(&mut self, text: T) -> &mut Self
Set UTF-8 text body (will be encoded per Content-Type on send).
Sourcepub fn body_json<V: Serialize>(&mut self, value: &V) -> &mut Self
pub fn body_json<V: Serialize>(&mut self, value: &V) -> &mut Self
Set JSON body (serialised via serde_json).
Sourcepub fn buffer_size(&mut self, n: usize) -> &mut Self
pub fn buffer_size(&mut self, n: usize) -> &mut Self
Set per-read buffer size.
Sourcepub fn http1_1_only(&mut self) -> &mut Self
pub fn http1_1_only(&mut self) -> &mut Self
Force HTTP/1.1.
Sourcepub fn http2_only(&mut self) -> &mut Self
pub fn http2_only(&mut self) -> &mut Self
Force HTTP/2.
Sourcepub fn no_redirect(&mut self) -> &mut Self
pub fn no_redirect(&mut self) -> &mut Self
Disable auto-follow of 3xx redirects (default).
Sourcepub fn max_redirect_times(&mut self, n: usize) -> &mut Self
pub fn max_redirect_times(&mut self, n: usize) -> &mut Self
Maximum number of redirects to follow (default DEFAULT_MAX_REDIRECT_TIMES).
Sourcepub fn decode(&mut self) -> &mut Self
pub fn decode(&mut self) -> &mut Self
Enable automatic response body decompression (gzip / deflate / br).
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).
Sourcepub fn build(&mut self) -> HttpRequest
pub fn build(&mut self) -> HttpRequest
Finalise the builder and return the HttpRequest.