pub struct HttpClientBuilder { /* private fields */ }Expand description
Builder for constructing an [HttpClient] with a layered tower middleware stack.
Implementations§
Source§impl HttpClientBuilder
impl HttpClientBuilder
Sourcepub fn with_config(config: HttpClientConfig) -> Self
pub fn with_config(config: HttpClientConfig) -> Self
Create a builder with a specific configuration
Sourcepub fn timeout(self, timeout: Duration) -> Self
pub fn timeout(self, timeout: Duration) -> Self
Set the per-request timeout
This timeout applies to each individual HTTP request/attempt. If retries are enabled, each retry attempt gets its own timeout.
Sourcepub fn total_timeout(self, timeout: Duration) -> Self
pub fn total_timeout(self, timeout: Duration) -> Self
Set the total timeout spanning all retry attempts
When set, the entire operation (including all retries and backoff delays)
must complete within this duration. If the deadline is exceeded,
the request fails with HttpError::DeadlineExceeded(total_timeout).
Sourcepub fn user_agent(self, user_agent: impl Into<String>) -> Self
pub fn user_agent(self, user_agent: impl Into<String>) -> Self
Set the user agent string
Sourcepub fn retry(self, retry: Option<RetryConfig>) -> Self
pub fn retry(self, retry: Option<RetryConfig>) -> Self
Set the retry configuration
Sourcepub fn max_body_size(self, size: usize) -> Self
pub fn max_body_size(self, size: usize) -> Self
Set the maximum response body size
Sourcepub fn transport(self, transport: TransportSecurity) -> Self
pub fn transport(self, transport: TransportSecurity) -> Self
Set transport security mode
Use TransportSecurity::AllowInsecureHttp only for testing with mock servers.
Sourcepub fn allow_insecure_http(self) -> Self
pub fn allow_insecure_http(self) -> Self
Allow insecure HTTP connections (for testing only)
Equivalent to .transport(TransportSecurity::AllowInsecureHttp).
WARNING: This should only be used for local testing with mock servers. Never use in production as it exposes traffic to interception.
§Compile-time Safety
This method is only available in debug builds or when the allow-insecure-http
feature is explicitly enabled. This prevents accidental use in production.
To use in release builds (e.g., for integration tests), add:
[features]
allow-insecure-http = []Sourcepub fn with_otel(self) -> Self
pub fn with_otel(self) -> Self
Enable OpenTelemetry tracing layer
When enabled, creates spans for outbound requests with HTTP metadata
and injects W3C trace context headers (when otel feature is enabled).
Sourcepub fn with_auth_layer(
self,
wrap: impl FnOnce(BoxCloneService<Request<Full<Bytes>>, Response<ResponseBody>, HttpError>) -> BoxCloneService<Request<Full<Bytes>>, Response<ResponseBody>, HttpError> + Send + 'static,
) -> Self
pub fn with_auth_layer( self, wrap: impl FnOnce(BoxCloneService<Request<Full<Bytes>>, Response<ResponseBody>, HttpError>) -> BoxCloneService<Request<Full<Bytes>>, Response<ResponseBody>, HttpError> + Send + 'static, ) -> Self
Insert an optional auth layer between retry and timeout in the stack.
Stack position: … → Retry → **this layer** → Timeout → …
The layer sits inside the retry loop so each attempt re-executes it (e.g. re-reads a refreshed bearer token). Only one auth layer can be set; a second call replaces the first.
Sourcepub fn buffer_capacity(self, capacity: usize) -> Self
pub fn buffer_capacity(self, capacity: usize) -> Self
Set the buffer capacity for concurrent request handling
The HTTP client uses an internal buffer to allow concurrent requests without external locking. This sets the maximum number of requests that can be queued.
Note: A capacity of 0 is invalid and will be clamped to 1. Tower’s Buffer panics with capacity=0, so we enforce minimum of 1.
Sourcepub fn max_redirects(self, max_redirects: usize) -> Self
pub fn max_redirects(self, max_redirects: usize) -> Self
Set the maximum number of redirects to follow
Set to 0 to disable redirect following (3xx responses pass through as-is).
Default: 10
Sourcepub fn no_redirects(self) -> Self
pub fn no_redirects(self) -> Self
Disable redirect following
Equivalent to .max_redirects(0). When disabled, 3xx responses are
returned to the caller without following the Location header.
Sourcepub fn redirect(self, config: RedirectConfig) -> Self
pub fn redirect(self, config: RedirectConfig) -> Self
Set the redirect policy configuration
Use this to configure redirect security settings:
same_origin_only: Only follow redirects to the same hoststrip_sensitive_headers: RemoveAuthorization/Cookieon cross-originallow_https_downgrade: Allow HTTPS → HTTP redirects (not recommended)
§Example
let client = HttpClient::builder()
.redirect(RedirectConfig::permissive()) // Allow all redirects with header stripping
.build()?;Sourcepub fn pool_idle_timeout(self, timeout: Option<Duration>) -> Self
pub fn pool_idle_timeout(self, timeout: Option<Duration>) -> Self
Set the idle connection timeout for the connection pool
Connections that remain idle for longer than this duration will be closed and removed from the pool. Default: 90 seconds.
Set to None to disable idle timeout (connections kept indefinitely).
Sourcepub fn pool_max_idle_per_host(self, max: usize) -> Self
pub fn pool_max_idle_per_host(self, max: usize) -> Self
Set the maximum number of idle connections per host
Limits how many idle connections are kept in the pool for each host. Default: 32.
- Setting to
0disables connection reuse entirely - Setting too high may waste resources on rarely-used connections
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for HttpClientBuilder
impl !RefUnwindSafe for HttpClientBuilder
impl Send for HttpClientBuilder
impl !Sync for HttpClientBuilder
impl Unpin for HttpClientBuilder
impl UnsafeUnpin for HttpClientBuilder
impl !UnwindSafe for HttpClientBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn decompression(self) -> Decompression<Self>where
Self: Sized,
fn decompression(self) -> Decompression<Self>where
Self: Sized,
Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
Source§fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
x-request-id as the header name. Read moreSource§fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
413 Payload Too Large responses. Read more