pub struct ClientResponse { /* private fields */ }Expand description
An HTTP client response.
Implementations§
Source§impl ClientResponse
impl ClientResponse
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the response status.
Sourcepub fn header<N: AsName>(&self, name: N) -> Option<&HeaderValue>
pub fn header<N: AsName>(&self, name: N) -> Option<&HeaderValue>
Returns the first header value associated with name.
Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns mutable access to the response headers.
Sourcepub fn set_payload(&self, payload: Payload)
pub fn set_payload(&self, payload: Payload)
Replaces the response payload.
Any unread previous payload is dropped. The response can be read again even if its previous payload was already taken.
Sourcepub fn take_payload(&self) -> Payload
pub fn take_payload(&self) -> Payload
Takes the response payload.
Subsequent calls return an empty payload. After the payload is taken,
body(), json() and the response stream
report PayloadError::AlreadyTaken instead of an empty body.
Sourcepub fn extensions(&self) -> Ref<'_, Extensions>
pub fn extensions(&self) -> Ref<'_, Extensions>
Returns the response extensions.
Sourcepub fn extensions_mut(&self) -> RefMut<'_, Extensions>
pub fn extensions_mut(&self) -> RefMut<'_, Extensions>
Returns mutable access to the response extensions.
Source§impl ClientResponse
impl ClientResponse
Sourcepub fn body(&self) -> MessageBody ⓘ
pub fn body(&self) -> MessageBody ⓘ
Returns a future that buffers the response body.
The response payload is taken when this method is called, not when the
future is first polled. Dropping the future without awaiting it
discards the body. Once the payload has been taken, by this method,
json(), take_payload() or the
response stream, a later body() fails with
PayloadError::AlreadyTaken. A response without a body still yields
empty bytes. That includes bodiless responses (HEAD requests, 204,
304): their Content-Length is not checked against the limit.
To keep the raw body when JSON parsing fails, buffer it once and parse the retained bytes:
let body = res.body().await?;
match serde_json::from_slice::<serde_json::Value>(&body) {
Ok(value) => println!("json: {value}"),
Err(_) => println!("raw: {} bytes", body.len()),
}Sourcepub fn json<T: DeserializeOwned>(&self) -> JsonBody<T> ⓘ
pub fn json<T: DeserializeOwned>(&self) -> JsonBody<T> ⓘ
Returns a future that buffers and deserializes a JSON response body.
The future returns an error when:
- the content type is not JSON;
- the body exceeds the configured response payload limit; or
- reading or deserializing the body fails.
A JSON content type is checked before the payload is taken, so a
content-type error leaves the body available. Otherwise the payload is
taken when this method is called, as with body(): a
dropped future or a deserialization failure leaves no body, and later
reads fail with PayloadError::AlreadyTaken. Use body() and
serde_json::from_slice() to keep the raw bytes.
Trait Implementations§
Source§impl Debug for ClientResponse
impl Debug for ClientResponse
Source§impl From<ServiceResponse> for ClientResponse
impl From<ServiceResponse> for ClientResponse
Source§fn from(res: ServiceResponse) -> Self
fn from(res: ServiceResponse) -> Self
Source§impl HttpMessage for ClientResponse
impl HttpMessage for ClientResponse
Parses cookies from the response Set-Cookie headers.
Headers that are not valid UTF-8 or cannot be parsed are skipped.
§Errors
This implementation does not return an error.
Source§fn message_headers(&self) -> &HeaderMap
fn message_headers(&self) -> &HeaderMap
Source§fn message_extensions(&self) -> Ref<'_, Extensions>
fn message_extensions(&self) -> Ref<'_, Extensions>
Source§fn message_extensions_mut(&self) -> RefMut<'_, Extensions>
fn message_extensions_mut(&self) -> RefMut<'_, Extensions>
Source§fn content_type(&self) -> &str
fn content_type(&self) -> &str
Content-Type header. Read moreSource§fn encoding(&self) -> Result<&'static Encoding, ContentTypeError>
fn encoding(&self) -> Result<&'static Encoding, ContentTypeError>
Content-Type. Read moreSource§fn mime_type(&self) -> Result<Option<Mime>, ContentTypeError>
fn mime_type(&self) -> Result<Option<Mime>, ContentTypeError>
Content-Type header as a MIME type. Read more