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: StringHTTP 版本 (如 “HTTP/1.1”)
headers: HashMap<String, Vec<String>>响应 headers (支持多值)
body: Option<Vec<u8>>可选的响应体
Implementations§
Source§impl HttpResponse
impl HttpResponse
Sourcepub fn from_bytes(data: &[u8]) -> Result<Self>
pub fn from_bytes(data: &[u8]) -> Result<Self>
Sourcepub fn header_all(&self, name: &str) -> Vec<String>
pub fn header_all(&self, name: &str) -> Vec<String>
Sourcepub fn content_length(&self) -> Option<u64>
pub fn content_length(&self) -> Option<u64>
Sourcepub fn is_redirect(&self) -> bool
pub fn is_redirect(&self) -> bool
Sourcepub fn location(&self) -> Option<Url>
pub fn location(&self) -> Option<Url>
获取 Location header 并解析为 URL
对于重定向响应特别有用。如果是相对 URL,会基于当前请求 URL 进行解析。
§Returns
解析后的绝对 URL,如果不存在或解析失败则返回 None
Sourcepub fn decoded_body(&self) -> Result<Vec<u8>>
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
impl Clone for HttpResponse
Source§fn clone(&self) -> HttpResponse
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for HttpResponse
impl RefUnwindSafe for HttpResponse
impl Send for HttpResponse
impl Sync for HttpResponse
impl Unpin for HttpResponse
impl UnsafeUnpin for HttpResponse
impl UnwindSafe for HttpResponse
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