pub struct RequestBuilder { /* private fields */ }Expand description
HTTP request builder with fluent API
Created by [HttpClient::get], [HttpClient::post], etc.
Supports chaining headers and body configuration before sending
with send().
§URL Construction
This crate does not provide query-string composition. Build your URL
externally (e.g. via url::Url) and pass the final string to HttpClient:
use url::Url;
use toolkit_http::HttpClient;
let mut url = Url::parse("https://api.example.com/users")?;
url.query_pairs_mut()
.append_pair("page", "1")
.append_pair("limit", "10");
let client = HttpClient::builder().build()?;
let resp = client.get(url.as_str()).send().await?;§Example
use toolkit_http::HttpClient;
let client = HttpClient::builder().build()?;
// Simple GET
let resp = client
.get("https://api.example.com/users")
.send()
.await?;
// POST with JSON body
let resp = client
.post("https://api.example.com/users")
.header("x-request-id", "123")
.json(&NewUser { name: "Alice" })?
.send()
.await?;
// POST with form body
let resp = client
.post("https://auth.example.com/token")
.header("authorization", "Basic xyz")
.form(&[("grant_type", "client_credentials")])?
.send()
.await?;Implementations§
Source§impl RequestBuilder
impl RequestBuilder
Sourcepub fn with_request_type(self, t: impl Into<Cow<'static, str>>) -> Self
pub fn with_request_type(self, t: impl Into<Cow<'static, str>>) -> Self
Attach a request_type label for metrics.
The label is added as a request_type attribute on the
http.client.request.duration histogram when the otel feature and a
metrics layer are configured. This mirrors go-appkit’s
NewContextWithRequestType / GetRequestTypeFromContext pattern and lets
you break down metrics by logical operation rather than route alone.
Setting this without a metrics layer is a safe no-op.
§Example
client
.get("https://api.example.com/tenants/123")
.with_request_type("tenants_resolve")
.send()
.await?;Sourcepub fn json<T: Serialize>(self, body: &T) -> Result<Self, HttpError>
pub fn json<T: Serialize>(self, body: &T) -> Result<Self, HttpError>
Set request body as JSON
Serializes the value using serde_json and sets Content-Type to application/json.
unless a Content-Type header was already provided.
§Errors
Returns Err(HttpError::Json) if serialization fails.
§Example
#[derive(Serialize)]
struct CreateUser { name: String }
let resp = client
.post("https://api.example.com/users")
.json(&CreateUser { name: "Alice".into() })?
.send()
.await?;Sourcepub fn form(self, fields: &[(&str, &str)]) -> Result<Self, HttpError>
pub fn form(self, fields: &[(&str, &str)]) -> Result<Self, HttpError>
Set request body as form URL-encoded
Serializes the fields and sets Content-Type to application/x-www-form-urlencoded. unless a Content-Type header was already provided.
§Errors
Returns Err(HttpError::FormEncode) if encoding fails.
§Example
let resp = client
.post("https://auth.example.com/token")
.form(&[
("grant_type", "client_credentials"),
("client_id", "my-app"),
])?
.send()
.await?;Sourcepub fn body_bytes(self, body: Bytes) -> Self
pub fn body_bytes(self, body: Bytes) -> Self
Sourcepub fn body_string(self, body: String) -> Self
pub fn body_string(self, body: String) -> Self
Sourcepub async fn send(self) -> Result<HttpResponse, HttpError>
pub async fn send(self) -> Result<HttpResponse, HttpError>
Send the request and return the response
§Errors
Returns HttpError if:
- Request building failed (invalid headers, URL, etc.)
- URL scheme is invalid for the transport security mode
- Network/transport error
- Request timeout
- Concurrency limit reached (
Overloaded)
§Example
let resp = client
.get("https://api.example.com/data")
.send()
.await?;
let data: MyData = resp.json().await?;Auto Trait Implementations§
impl !Freeze for RequestBuilder
impl !RefUnwindSafe for RequestBuilder
impl !UnwindSafe for RequestBuilder
impl Send for RequestBuilder
impl Sync for RequestBuilder
impl Unpin for RequestBuilder
impl UnsafeUnpin for RequestBuilder
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> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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 catch_panic(self) -> CatchPanic<Self, DefaultResponseForPanic>where
Self: Sized,
fn catch_panic(self) -> CatchPanic<Self, DefaultResponseForPanic>where
Self: Sized,
500 Internal Server responses. 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