Skip to main content

HttpResponse

Struct HttpResponse 

Source
pub struct HttpResponse {
    pub status_code: u16,
    pub reason_phrase: String,
    pub version: String,
    pub headers: HashMap<String, Vec<String>>,
    pub body: Option<Vec<u8>>,
}
Expand description

HTTP 响应结构体

表示一个完整的 HTTP 响应,包含状态码、reason phrase、版本、headers 和可选的 body。 支持多值 headers (如 Set-Cookie)。

Fields§

§status_code: u16

状态码 (如 200, 404, 301)

§reason_phrase: String

原因短语 (如 OK, Not Found, Moved Permanently)

§version: String

HTTP 版本 (如 “HTTP/1.1”)

§headers: HashMap<String, Vec<String>>

响应 headers (支持多值)

§body: Option<Vec<u8>>

可选的响应体

Implementations§

Source§

impl HttpResponse

Source

pub fn from_bytes(data: &[u8]) -> Result<Self>

从原始字节解析 HTTP 响应

解析符合 HTTP/1.1 规范的响应数据,包括状态行、headers 和 body。 支持多值 headers (通过逗号分隔或多个同名 header)。

§Arguments
  • data - 原始 HTTP 响应字节
§Returns

解析后的 HttpResponse,或错误信息

§Errors

返回错误如果响应格式无效或无法解析

Source

pub fn header(&self, name: &str) -> Option<&String>

获取指定 header 的第一个值

§Arguments
  • name - header 名称 (不区分大小写)
§Returns

第一个 header 值的引用,如果不存在则返回 None

Source

pub fn header_all(&self, name: &str) -> Vec<String>

获取指定 header 的所有值

对于 Set-Cookie 等可能多次出现的 header 特别有用。

§Arguments
  • name - header 名称 (不区分大小写)
§Returns

包含所有匹配值的向量

Source

pub fn content_length(&self) -> Option<u64>

获取 Content-Length header 的值

§Returns

内容长度 (u64),如果不存在或解析失败则返回 None

Source

pub fn is_redirect(&self) -> bool

检查是否为重定向响应 (3xx)

§Returns

如果状态码在 300-399 范围内返回 true

Source

pub fn location(&self) -> Option<Url>

获取 Location header 并解析为 URL

对于重定向响应特别有用。如果是相对 URL,会基于当前请求 URL 进行解析。

§Returns

解析后的绝对 URL,如果不存在或解析失败则返回 None

Source

pub fn decoded_body(&self) -> Result<Vec<u8>>

使用流式解码器获取解码后的 body

根据 HTTP 响应的 Content-Encoding 和 Transfer-Encoding headers 自动选择合适的解码器, 对响应体进行解码。支持 GZip、Chunked、BZip2 等编码格式。

遵循 RFC 7230 Section 3.3.1 规范:Transfer-Encoding 优先于 Content-Encoding。

§Returns

解码后的原始数据,或错误信息。如果没有 body 则返回空向量。

§Errors
  • 如果编码格式无效或数据损坏
  • 如果解码过程中发生 I/O 错误
§Examples
let response = /* HTTP response with Content-Encoding: gzip */;
let decoded = response.decoded_body()?;
// decoded 包含解压后的原始数据

Trait Implementations§

Source§

impl Clone for HttpResponse

Source§

fn clone(&self) -> HttpResponse

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HttpResponse

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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