Skip to main content

RequestBuilder

Struct RequestBuilder 

Source
pub struct RequestBuilder<'a> { /* private fields */ }
Available on crate features async-tls-native or async-tls-rustls-aws-lc-rs or async-tls-rustls-ring only.
Expand description

Builds and executes a single request against an existing Client.

Create a builder from Client::request or the verb helpers such as Client::get and Client::post.

See also:

  • examples/request_helpers.rs
  • examples/request_overrides.rs
  • examples/streaming.rs

§Example

use reqx::prelude::Client;

let client = Client::builder("https://api.example.com").build()?;
let response = client
    .post("/v1/items")
    .idempotency_key("item-1")?
    .query_pair("verbose", "true")
    .json(&serde_json::json!({ "name": "demo" }))?
    .send_response()
    .await?;

let _status = response.status();

Implementations§

Source§

impl<'a> RequestBuilder<'a>

Source

pub fn header(self, name: HeaderName, value: HeaderValue) -> Self

Available on crate feature _async only.

Adds a header to this request.

See also examples/request_helpers.rs.

Source

pub fn try_header(self, name: &str, value: &str) -> Result<Self>

Available on crate feature _async only.

Parses and adds a header to this request.

Source

pub fn idempotency_key(self, key: &str) -> Result<Self>

Available on crate feature _async only.

Sets the Idempotency-Key header for retry-safe mutations.

Source

pub fn query_pair( self, name: impl Into<String>, value: impl Into<String>, ) -> Self

Available on crate feature _async only.

Appends one query parameter pair.

Source

pub fn query_pairs<K, V, I>(self, pairs: I) -> Self
where K: Into<String>, V: Into<String>, I: IntoIterator<Item = (K, V)>,

Available on crate feature _async only.

Appends multiple query parameter pairs.

Source

pub fn query<T>(self, params: &T) -> Result<Self>
where T: Serialize + ?Sized,

Available on crate feature _async only.

Serializes and appends query parameters from params.

Source

pub fn body(self, body: impl Into<Bytes>) -> Self

Available on crate feature _async only.

Sets a fully buffered request body.

Source

pub fn body_stream<S, E>(self, stream: S) -> Self
where S: Stream<Item = Result<Bytes, E>> + Send + 'static, E: StdError + Send + Sync + 'static,

Available on crate feature _async only.

Sets a streaming request body.

Source

pub fn body_reader<R>(self, reader: R) -> Self
where R: AsyncRead + Send + 'static,

Available on crate feature _async only.

Streams an async reader as the request body.

See also examples/streaming.rs.

Source

pub fn body_reader_with_length<R>( self, reader: R, content_length: u64, ) -> Result<Self>
where R: AsyncRead + Send + 'static,

Available on crate feature _async only.

Streams an async reader as the request body and sets Content-Length.

Source

pub fn json<T>(self, payload: &T) -> Result<Self>
where T: Serialize + ?Sized,

Available on crate feature _async only.

Serializes payload as JSON and sets Content-Type: application/json.

See also examples/basic_json.rs.

Source

pub fn form<T>(self, payload: &T) -> Result<Self>
where T: Serialize + ?Sized,

Available on crate feature _async only.

Serializes payload as form data and sets the form content type.

Source

pub fn timeout(self, timeout: Duration) -> Self

Available on crate feature _async only.

Overrides the per-attempt request timeout for this request.

A zero duration is rejected by send or send_stream.

Source

pub fn total_timeout(self, total_timeout: Duration) -> Self

Available on crate feature _async only.

Overrides the overall deadline for this request.

A zero duration is rejected by send or send_stream.

Source

pub fn max_response_body_bytes(self, max_response_body_bytes: usize) -> Self

Available on crate feature _async only.

Overrides the buffered response body size limit for this request.

Source

pub fn retry_policy(self, retry_policy: RetryPolicy) -> Self

Available on crate feature _async only.

Overrides the retry policy for this request.

See also examples/request_overrides.rs.

Source

pub fn redirect_policy(self, redirect_policy: RedirectPolicy) -> Self

Available on crate feature _async only.

Overrides redirect handling for this request.

Source

pub fn status_policy(self, status_policy: StatusPolicy) -> Self

Available on crate feature _async only.

Overrides status handling for this request.

Source

pub fn auto_accept_encoding(self, enabled: bool) -> Self

Available on crate feature _async only.

Overrides automatic Accept-Encoding injection for this request.

Source

pub async fn send(self) -> Result<Response>

Available on crate feature _async only.

Executes the request and applies the effective StatusPolicy.

Source

pub async fn send_stream(self) -> Result<ResponseStream>

Available on crate feature _async only.

Executes the request and returns a streaming response body.

Non-success HTTP statuses still follow the effective StatusPolicy. See also examples/streaming.rs.

Source

pub async fn download_to_writer<W>(self, writer: &mut W) -> Result<u64>
where W: AsyncWrite + Unpin + Send + ?Sized,

Available on crate feature _async only.

Streams the response body into writer.

See also examples/streaming.rs.

Source

pub async fn download_to_writer_limited<W>( self, writer: &mut W, max_bytes: usize, ) -> Result<u64>
where W: AsyncWrite + Unpin + Send + ?Sized,

Available on crate feature _async only.

Streams the response body into writer, enforcing max_bytes.

See also examples/streaming.rs.

Source

pub async fn send_json<T>(self) -> Result<T>

Available on crate feature _async only.

Executes the request, buffers the body, and deserializes it as JSON.

Source

pub async fn send_response(self) -> Result<Response>

Available on crate feature _async only.

Executes the request and always returns a buffered crate::Response for HTTP status responses.

Source

pub async fn send_response_stream(self) -> Result<ResponseStream>

Available on crate feature _async only.

Executes the request and always returns a streaming response for HTTP status responses.

Auto Trait Implementations§

§

impl<'a> !Freeze for RequestBuilder<'a>

§

impl<'a> !RefUnwindSafe for RequestBuilder<'a>

§

impl<'a> !Sync for RequestBuilder<'a>

§

impl<'a> !UnwindSafe for RequestBuilder<'a>

§

impl<'a> Send for RequestBuilder<'a>

§

impl<'a> Unpin for RequestBuilder<'a>

§

impl<'a> UnsafeUnpin for RequestBuilder<'a>

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> 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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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