pub struct HttpResponse { /* private fields */ }Expand description
One response, with its body already read.
Implementations§
Source§impl HttpResponse
impl HttpResponse
Sourcepub fn new(raw: RawResponse) -> Self
pub fn new(raw: RawResponse) -> Self
Wrap what a transport returned.
Sourcepub fn with(status: u16, body: impl Into<Vec<u8>>) -> Self
pub fn with(status: u16, body: impl Into<Vec<u8>>) -> Self
Build one directly. For a fake, and for a test.
Sourcepub fn with_header(
self,
name: impl Into<String>,
value: impl Into<String>,
) -> Self
pub fn with_header( self, name: impl Into<String>, value: impl Into<String>, ) -> Self
Add a header.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Whether the status is 2xx.
Sourcepub fn is_client_error(&self) -> bool
pub fn is_client_error(&self) -> bool
Whether the status is 4xx.
Sourcepub fn is_server_error(&self) -> bool
pub fn is_server_error(&self) -> bool
Whether the status is 5xx.
Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T>
pub fn json<T: DeserializeOwned>(&self) -> Result<T>
The body, deserialised.
§Errors
Quoting the start of the body, because a parse failure here is almost
always an error page or a rate-limit notice where JSON was expected,
and expected value at line 1 column 1 says nothing about which.
Sourcepub fn error_for_status(self) -> Result<Self>
pub fn error_for_status(self) -> Result<Self>
Turn a 4xx or 5xx into an error.
Not automatic, and deliberately: plenty of callers want to look at a
404 rather than propagate it, and a client that made that decision
for them would have them parsing the error back apart.
The message carries the status and the start of the body, which is where the other end put the reason.