Overview
The HTTP client abstraction provides a runtime-agnostic interface for making HTTP requests. It’s primarily used for:- Media uploads to WhatsApp servers
- Media downloads (with streaming support)
- Fetching metadata and authentication tokens
HttpClient Trait
Methods
supports_streaming
Returns whether this HTTP client supports synchronous streaming downloads viaexecute_streaming. The default implementation returns false. When this returns false, download_to_writer automatically falls back to a buffered download using execute instead.
trueifexecute_streamingis implemented (e.g.,UreqHttpClient)false(default) if streaming is not supported
execute
Executes an HTTP request and buffers the entire response body.request: HttpRequest- The request to execute
HttpResponsewith buffered body on successanyhow::Erroron failure
execute_streaming
Executes an HTTP request and returns a streaming reader over the response body. Important: This is a synchronous method that must be called from withintokio::task::spawn_blocking.
request: HttpRequest- The request to execute
StreamingHttpResponsewith streaming body readeranyhow::Erroron failure or if streaming is not supported
Data Structures
HttpRequest
Represents an HTTP request with headers and optional body.Constructors
Builder Methods
HttpResponse
Represents an HTTP response with buffered body.Methods
StreamingHttpResponse
Represents an HTTP response with streaming body reader.UreqHttpClient
The default HTTP client implementation using theureq crate (v3.3) for synchronous HTTP requests.
Features
- Blocking I/O - Uses synchronous ureq, wrapped in
tokio::task::spawn_blocking - Connection pooling - Shares a
ureq::Agentacross requests for connection reuse - Streaming support - Implements efficient streaming downloads
- Simple API - Minimal configuration required
- Thread-safe - Implements
Clonefor easy sharing (cloning theAgentis cheap) - TLS via rustls - Uses rustls for TLS, with optional
danger-skip-tls-verifyfor testing
Creating a client
with_agent
ureq::Agent. This lets you configure proxy support, custom TLS, timeouts, or any other agent-level settings externally.
This is the primary extension point for customizing HTTP behavior — for example, routing media uploads and downloads through a proxy, or using custom CA certificates.
Parameters:
agent- A pre-configuredureq::Agent
Usage Examples
Basic GET Request
POST Request with Body
Streaming Download
Internal Implementation
TheUreqHttpClient wraps a shared ureq::Agent for connection pooling. All requests go through the agent rather than standalone ureq::get()/ureq::post() functions:
danger-skip-tls-verify feature is enabled, the agent is built with TLS verification disabled:
execute method clones the agent and wraps the call in spawn_blocking:
execute_streaming method is synchronous (no spawn_blocking) because it’s called from within a blocking context. It also uses the shared agent:
Implementing Custom HTTP Clients
You can implement custom HTTP clients for different runtimes or requirements.Example: Reqwest Client (Async)
Example: Mock Client for Testing
Usage with Bot builder
Best Practices
- Blocking operations - Always wrap blocking HTTP libraries in
tokio::task::spawn_blocking - Streaming for large files - Use
execute_streamingfor media downloads to avoid buffering - Error handling - Return descriptive errors with context
- Timeouts - ureq 3.3 applies per-IP connection timeouts automatically; implement request-level timeouts for reliability
- Retries - Consider retry logic for transient failures
- Connection pooling - Use a shared
ureq::Agent(asUreqHttpClientdoes) for connection reuse
Media Operations
The HTTP client is primarily used for media operations in whatsapp-rust:Media Upload Flow
The client manages media connections internally. Use the high-levelupload method instead of building requests manually:
- Fetch media connection credentials from WhatsApp servers
- Encrypt the media with AES-256-CBC
- Upload to the CDN with proper auth headers
- Parse the response for
direct_pathand file hashes
Media Download Flow
Testing
Unit Test Example
See Also
- Storage Traits - Storage backend abstraction
- Transport Trait - Network transport abstraction
- Client API - Main client interface
- Media Handling - Guide to sending and receiving media