Skip to main content

RequestBuilder

Struct RequestBuilder 

Source
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

Source

pub fn get_request_mut(&mut self) -> &mut HttpRequest

Returns the underlying request mutably.

§Returns
  • &mut HttpRequest - The mutable request being built.
Source

pub fn new() -> Self

Create an empty builder (defaults: GET, no headers, default config).

Source

pub fn get(&mut self, url: impl Into<String>) -> &mut Self

Shortcut for method(Method::Get) + url(url).

Source

pub fn post(&mut self, url: impl Into<String>) -> &mut Self

Shortcut for method(Method::Post) + url(url).

Source

pub fn method(&mut self, method: Method) -> &mut Self

Set HTTP method explicitly (Method::Get / Method::Post / etc.).

Source

pub fn url(&mut self, url: impl Into<String>) -> &mut Self

Set URL.

Source

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).

Source

pub fn headers<K, V>(&mut self, headers: HashMap<K, V>) -> &mut Self
where K: AsRef<str>, V: AsRef<str>,

Set many headers at once.

Source

pub fn remove_header<K: AsRef<str>>(&mut self, key: K) -> &mut Self

Remove a header by key.

Source

pub fn clear_headers(&mut self) -> &mut Self

Clear all headers.

Source

pub fn body<B: Into<Vec<u8>>>(&mut self, bytes: B) -> &mut Self

Set raw body bytes.

Source

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).

Source

pub fn body_json<V: Serialize>(&mut self, value: &V) -> &mut Self

Set JSON body (serialised via serde_json).

Source

pub fn timeout(&mut self, ms: u64) -> &mut Self

Set request timeout in milliseconds.

Source

pub fn buffer_size(&mut self, n: usize) -> &mut Self

Set per-read buffer size.

Source

pub fn http1_1_only(&mut self) -> &mut Self

Force HTTP/1.1.

Source

pub fn http2_only(&mut self) -> &mut Self

Force HTTP/2.

Source

pub fn redirect(&mut self) -> &mut Self

Enable auto-follow of 3xx redirects.

Source

pub fn no_redirect(&mut self) -> &mut Self

Disable auto-follow of 3xx redirects (default).

Source

pub fn max_redirect_times(&mut self, n: usize) -> &mut Self

Maximum number of redirects to follow (default DEFAULT_MAX_REDIRECT_TIMES).

Source

pub fn decode(&mut self) -> &mut Self

Enable automatic response body decompression (gzip / deflate / br).

Source

pub fn no_decode(&mut self) -> &mut Self

Disable automatic response body decompression.

Source

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).

Source

pub fn no_proxy(&mut self) -> &mut Self

Clear the proxy (use direct connection).

Source

pub fn build(&mut self) -> HttpRequest

Finalise the builder and return the HttpRequest.

Trait Implementations§

Source§

impl Clone for RequestBuilder

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RequestBuilder

Source§

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

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

impl Default for RequestBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> AnySend for T
where T: Any + Send,

Source§

impl<T> AnySendClone for T
where T: Any + Send + Clone,

Source§

impl<T> AnySendSync for T
where T: Any + Send + Sync,

Source§

impl<T> AnySendSyncClone for T
where T: Any + Send + Sync + Clone,

Source§

impl<T> AnySync for T
where T: Any + Sync,

Source§

impl<T> AnySyncClone for T
where T: Any + Sync + Clone,

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.