pub struct RequestBuilder { /* private fields */ }Expand description
HTTP 请求构建器
提供链式调用接口构建 HTTP 请求。
§Header 错误处理
header、headers 等方法在解析失败时不会立即返回错误,而是将错误信息
累积到内部 header_errors 列表中。调用 send() 时,若存在累积的 header
错误,则返回 BaseError::ParamInvalid("header", ...) 而不发送请求。
§示例
ⓘ
// tools 为启动期经 ToolsBuilder::http(...) 冻结的应用资源;Action 内则用 ctx.http()?
let response = tools.http()?
.get("https://api.example.com/users")
.header("X-Custom-Header", "value")
.query("page", "1")
.bearer_token("your_token")
.timeout(60)
.send()
.await?;Implementations§
Source§impl RequestBuilder
impl RequestBuilder
Sourcepub fn content_type(self, content_type: &str) -> Self
pub fn content_type(self, content_type: &str) -> Self
Sourcepub fn bearer_token(self, token: &str) -> Self
pub fn bearer_token(self, token: &str) -> Self
Sourcepub fn user_agent(self, user_agent: &str) -> Self
pub fn user_agent(self, user_agent: &str) -> Self
Sourcepub fn json<T: Serialize>(self, json: &T) -> Result<Self, BaseError>
pub fn json<T: Serialize>(self, json: &T) -> Result<Self, BaseError>
设置 JSON 请求体
§参数
json: 可序列化为 JSON 的数据
§返回
Ok(Self): 设置成功Err(BaseError): 序列化失败
§示例
ⓘ
#[derive(Serialize)]
struct User {
name: String,
email: String,
}
let user = User {
name: "Alice".to_string(),
email: "alice@example.com".to_string(),
};
let response = client
.post("https://api.example.com/users")
.json(&user)?
.send()
.await?;Sourcepub fn max_response_bytes(self, bytes: usize) -> Self
pub fn max_response_bytes(self, bytes: usize) -> Self
Sourcepub fn retry(self, config: RetryConfig) -> Self
pub fn retry(self, config: RetryConfig) -> Self
设置请求级重试策略(L-4)。
默认不重试。启用后,对连接错误与命中 retry_on 的状态码按指数退避重试。
非幂等方法(POST/PATCH)默认不参与重试,需
RetryConfig::retry_non_idempotent 置为 true 才显式承担重复写风险。
§参数
config: 重试策略
§示例
ⓘ
use yang_base::http::RetryConfig;
let response = client
.get("https://api.example.com/users")
.retry(RetryConfig::default())
.send()
.await?;Auto Trait Implementations§
impl !RefUnwindSafe for RequestBuilder
impl !UnwindSafe for RequestBuilder
impl Forget for RequestBuilder
impl Freeze for RequestBuilder
impl Move 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
Mutably borrows from an owned value. Read more
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more