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

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.

Source

pub fn new() -> Self

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

§Returns
  • Self - A fresh request builder with default settings.
Source

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

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

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

§Arguments
  • Method - The HTTP method to apply to the request.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Set URL.

§Arguments
  • T - A value convertible into the request URL, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn query<K, V>(&mut self, key: K, value: V) -> &mut Self
where K: AsRef<str>, V: AsRef<str>,

Append a percent-encoded query parameter to the request URL.

§Arguments
  • K - The query parameter name.
  • V - The query parameter value.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn query_pairs<I, K, V>(&mut self, pairs: I) -> &mut Self
where I: IntoIterator<Item = (K, V)>, K: AsRef<str>, V: AsRef<str>,

Append multiple percent-encoded query parameters to the request URL.

§Arguments
  • I - Iterator yielding (key, value) pairs.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
where K: AsRef<str>, V: AsRef<str>,

Set a single header (last write wins on duplicate keys).

§Arguments
  • K - A header name that can be borrowed as a string slice.
  • V - A header value that can be borrowed as a string slice.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
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.

§Arguments
  • HashMap<K, V> - The header name-value pairs to apply, where each name and value can be borrowed as a string slice.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn bearer_auth<T>(&mut self, token: T) -> &mut Self
where T: AsRef<str>,

Set an Authorization: Bearer <token> header.

§Arguments
  • T - The bearer token string.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn basic_auth<U, P>(&mut self, username: U, password: P) -> &mut Self
where U: AsRef<str>, P: AsRef<str>,

Set an Authorization: Basic <base64(username:password)> header.

§Arguments
  • U - The username string.
  • P - The password string.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn cookie<K, V>(&mut self, name: K, value: V) -> &mut Self
where K: AsRef<str>, V: AsRef<str>,

Append a name=value cookie to the outgoing Cookie header.

§Arguments
  • K - Cookie name.
  • V - Cookie value.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Remove a header by key.

§Arguments
  • K - The header name to remove, which can be borrowed as a string slice.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Clear all headers.

§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Set raw body bytes.

§Arguments
  • B - A value convertible into the raw body bytes, such as Vec<u8> or &[u8].
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn body_text<T>(&mut self, text: T) -> &mut Self
where T: Into<String>,

Set UTF-8 text body.

§Arguments
  • T - A value convertible into the text body, such as &str or String.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Set JSON body (serialised via serde_json).

§Arguments
  • &V - A reference to the value to serialise as the JSON body.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn body_form<I, K, V>(&mut self, fields: I) -> &mut Self
where I: IntoIterator<Item = (K, V)>, K: AsRef<str>, V: AsRef<str>,

Set an application/x-www-form-urlencoded body and matching Content-Type header.

§Arguments
  • I - Iterator yielding (key, value) form fields.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn body_multipart(&mut self, parts: &[BodyPart]) -> &mut Self

Set an RFC 7578 multipart/form-data body from http_type::BodyPart entries.

§Arguments
  • &[BodyPart] - Multipart fields and uploaded file parts.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn root_cert_pem( &mut self, pem_bytes: &[u8], ) -> Result<&mut Self, RequestError>

Add one or more PEM-encoded root CA certificates for TLS verification.

§Arguments
  • &[u8] - Raw PEM-encoded X.509 certificate bytes.
§Returns
  • Result<&mut Self, RequestError> - Updated builder or an error when PEM parsing fails.
Source

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

Set request timeout in milliseconds.

§Arguments
  • u64 - The request timeout in milliseconds.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Set dedicated TCP connect timeout in milliseconds (0 inherits timeout).

§Arguments
  • u64 - The connect timeout in milliseconds.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn retry(&mut self, max_retries: usize) -> &mut Self

Configure automatic bounded retries for idempotent transient failures (0 = disabled).

§Arguments
  • usize - Maximum number of retry attempts.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

pub fn keep_alive(&mut self, enabled: bool) -> &mut Self

Enable or disable HTTP keep-alive connection pooling for this request.

§Arguments
  • bool - true to reuse persistent HTTP/1.1 or HTTP/2 connections from the pool, false to close after use.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Set per-read buffer size.

§Arguments
  • usize - The per-read buffer size in bytes.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Force HTTP/1.1.

§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Force HTTP/2.

§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

Enable auto-follow of 3xx redirects.

§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

Maximum number of redirects to follow (default DEFAULT_MAX_REDIRECT_TIMES).

§Arguments
  • usize - The maximum number of redirects to follow.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

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

Disable automatic response body decompression.

§Returns
  • &mut Self - A mutable reference to this builder for chaining.
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).

§Arguments
  • Proxy - The proxy configuration to apply to the request.
§Returns
  • &mut Self - A mutable reference to this builder for chaining.
Source

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

pub fn build(&mut self) -> HttpRequest

Finalise the builder and return the HttpRequest.

§Returns
  • HttpRequest - The finalised request, leaving the builder empty.
Source§

impl RequestBuilder

Source

pub fn get_request(&self) -> &HttpRequest

Source

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

Source

pub fn set_request(&mut self, val: HttpRequest) -> &mut Self

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§

§

impl Forget for RequestBuilder

§

impl Freeze for RequestBuilder

§

impl Move for RequestBuilder

§

impl RefUnwindSafe for RequestBuilder

§

impl Send for RequestBuilder
where RawVecInner: Send, RawTable<(String, String)>: Send, RawTable<(String, ())>: Send, &'static [u8]: Send,

§

impl Sync for RequestBuilder
where RawVecInner: Sync, RawTable<(String, String)>: Sync, RawTable<(String, ())>: Sync,

§

impl Unpin for RequestBuilder
where *const u8 is !null: Unpin, &'static [u8]: Unpin,

§

impl UnsafeUnpin for RequestBuilder

§

impl UnwindSafe for RequestBuilder
where RawVecInner: UnwindSafe, HashMap<String, String>: UnwindSafe, NonNull<u8>: UnwindSafe, &'static [u8]: 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> 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more