Skip to main content

Response

Struct Response 

Source
pub struct Response { /* private fields */ }
Expand description

HTTP 响应

封装 reqwest::Response,提供便捷的响应处理方法

§示例

ⓘ
// tools 为启动期经 ToolsBuilder::http(...) 冻结的应用资源;Action 内则用 ctx.http()?
let response = tools.http()?
    .get("https://api.example.com/users")
    .send()
    .await?;

// 检查状态码
if response.is_success() {
    // 解析 JSON
    let users: Vec<User> = response.json().await?;
}

Implementations§

Source§

impl Response

Source

pub fn status(&self) -> u16

获取状态码

§返回
  • HTTP 状态码
§示例
ⓘ
let status = response.status();
println!("状态码: {}", status);
Source

pub fn is_success(&self) -> bool

检查响应是否成功(2xx)

§返回
  • true: 状态码为 2xx
  • false: 状态码不是 2xx
§示例
ⓘ
if response.is_success() {
    println!("请求成功");
} else {
    println!("请求失败,状态码: {}", response.status());
}
Source

pub fn headers(&self) -> &HeaderMap

获取响应头

§返回
  • 响应头映射
§示例
ⓘ
let headers = response.headers();
if let Some(content_type) = headers.get("content-type") {
    println!("Content-Type: {:?}", content_type);
}
Source

pub fn url(&self) -> &str

获取本次响应最终落地的 URL。

跟随重定向时返回最后一跳的 URL,便于调用方在 opt-in 场景下复核目标 host。该访问器只读;日志仍由请求侧统一脱敏(移除 query 与 userinfo), 请勿把未脱敏的 URL 直接写入日志。

§返回
  • 最终 URL 字符串
§示例
ⓘ
let final_url = response.url();
println!("最终地址: {}", final_url);
Source

pub async fn text(self) -> Result<String, BaseError>

获取响应体为文本

§返回
  • Ok(String): 响应文本
  • Err(BaseError): 解析失败
§示例
ⓘ
let text = response.text().await?;
println!("响应内容: {}", text);
Source

pub async fn bytes(self) -> Result<Vec<u8>, BaseError>

获取响应体为字节流

§返回
  • Ok(Vec<u8>): 响应字节
  • Err(BaseError): 解析失败
§示例
ⓘ
let bytes = response.bytes().await?;
println!("响应大小: {} 字节", bytes.len());
Source

pub async fn json<T: DeserializeOwned>(self) -> Result<T, BaseError>

获取响应体为 JSON

§类型参数
  • T: 目标类型,必须实现 serde::de::DeserializeOwned
§返回
  • Ok(T): 反序列化后的对象
  • Err(BaseError): 解析失败
§示例
ⓘ
#[derive(Deserialize)]
struct User {
    id: u64,
    name: String,
}

let user: User = response.json().await?;
println!("用户: {} (ID: {})", user.name, user.id);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more