This is an automated email from the ASF dual-hosted git repository. Xuanwo pushed a commit to branch xuanwo/document-reqwest-default-headers in repository https://gitbox.apache.org/repos/asf/opendal.git
commit 4e6051e5e10d82709ec1b51dcdcab3211496e933 Author: Xuanwo <[email protected]> AuthorDate: Mon Nov 30 15:25:50 2026 +0100 docs(core): Show how to set default request headers for one operator --- .../core/src/docs/performance/http_optimization.md | 26 +++++++++++++++++++--- core/core/src/types/http_transport/mod.rs | 5 +++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/core/core/src/docs/performance/http_optimization.md b/core/core/src/docs/performance/http_optimization.md index 825554056e..4286ef1e1f 100644 --- a/core/core/src/docs/performance/http_optimization.md +++ b/core/core/src/docs/performance/http_optimization.md @@ -10,9 +10,9 @@ before `main`. Applications that need a different process-wide default must disable `auto-register-services`, initialize the service registry when needed, and install their transport before another default is installed. -A simpler option is to replace the transport for one operator. Add -`opendal-http-transport-reqwest` and `reqwest` as direct dependencies, build one -client, and reuse the resulting operator: +To replace the transport for one operator, add `opendal`, +`opendal-http-transport-reqwest`, and `reqwest` 0.13 as direct dependencies. +Build one client and reuse the resulting operator. ```rust,ignore use opendal::{HttpTransporter, Operator}; @@ -28,6 +28,26 @@ fn with_reqwest_client(op: Operator, client: reqwest::Client) -> Operator { Creating a client or operator per request discards DNS and connection-pool state and repeatedly pays connection setup costs. +## Set default request headers + +Use `ClientBuilder::default_headers` on the reqwest client you install with +`with_reqwest_client` to set a default header for one operator. + +```rust,ignore +use reqwest::header::{HeaderMap, HeaderValue}; + +let mut headers = HeaderMap::new(); +headers.insert("x-client-label", HeaderValue::from_static("example")); +let client = reqwest::Client::builder().default_headers(headers).build()?; +let op = with_reqwest_client(op, client); +``` + +Reqwest fills in missing headers. Explicit request headers take precedence. +Use harmless values because defaults can reach other endpoints contacted by +the same client. Services that sign requests do so before handing them to the +transport. A header that must participate in the signature needs to be present +at signing time. + ## Prefer HTTP/1.1 for large transfers For high-throughput uploads and downloads of large objects, benchmark HTTP/1.1 diff --git a/core/core/src/types/http_transport/mod.rs b/core/core/src/types/http_transport/mod.rs index 9e00b29add..9a0f630105 100644 --- a/core/core/src/types/http_transport/mod.rs +++ b/core/core/src/types/http_transport/mod.rs @@ -47,6 +47,11 @@ static DEFAULT_HTTP_TRANSPORTER: OnceLock<HttpTransporter> = OnceLock::new(); /// Implement this trait to provide a custom HTTP backend. A transport must /// support 3xx redirection. /// It can support [`HttpRedirect`] to let services reuse redirect destinations. +/// +/// See [Set default request headers] for an example that configures a reqwest +/// client for one operator. +/// +/// [Set default request headers]: crate::docs::performance::http_optimization#set-default-request-headers pub trait HttpTransport: Send + Sync + Unpin + 'static { /// Fetch a request and return a streamable [`HttpBody`]. #[cfg(not(target_arch = "wasm32"))]
