pub struct Response {
pub status: u16,
pub headers: HeaderMap,
pub body: Vec<u8>,
/* private fields */
}Expand description
Fields§
§status: u16HTTP status (200, 404…).
headers: HeaderMapResponse headers.
body: Vec<u8>The body, read whole.
Implementations§
Source§impl Response
impl Response
Sourcepub fn new(status: u16, headers: HeaderMap, body: Vec<u8>) -> Self
pub fn new(status: u16, headers: HeaderMap, body: Vec<u8>) -> Self
A response with this status, headers and body (for tests of code that reads responses).
§Examples
let response = ocre::http::Response::new(404, Default::default(), Vec::new());
assert!(!response.is_success());Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Whether the status is 2xx.
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
The value of header name (case-insensitive), if present and text.
§Examples
use axum::http::HeaderMap;
let mut headers = HeaderMap::new();
headers.insert("ETag", "\"abc\"".parse().unwrap());
let response = ocre::http::Response::new(200, headers, Vec::new());
assert_eq!(response.header("etag"), Some("\"abc\""));Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
The body as text (invalid UTF-8 replaced).
§Examples
assert_eq!(ocre::http::Response::new(200, Default::default(), b"ok".to_vec()).text(), "ok");Sourcepub fn json<T: DeserializeOwned>(&self) -> Result<T>
pub fn json<T: DeserializeOwned>(&self) -> Result<T>
The body parsed as JSON.
§Errors
Error::Internal when the body is not
JSON of type T; the message quotes its first 200 characters.
§Examples
let response = ocre::http::Response::new(200, Default::default(), b"<html>".to_vec());
assert!(response.json::<serde_json::Value>().unwrap_err().to_string().contains("<html>"));Sourcepub fn error_for_status(self) -> Result<Self>
pub fn error_for_status(self) -> Result<Self>
self when the status is 2xx, else an error naming the request, the status and the start of the body.
§Errors
Error::Internal for a status outside 200-299.
§Examples
let response = ocre::http::Response::new(503, Default::default(), b"busy".to_vec());
assert_eq!(response.error_for_status().unwrap_err().to_string(), "internal error: the request answered 503: busy");Trait Implementations§
impl Eq for Response
impl StructuralPartialEq for Response
Auto Trait Implementations§
impl Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnsafeUnpin for Response
impl UnwindSafe for Response
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Checks if this value is equivalent to the given key. Read more
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Checks if this value is equivalent to the given key. Read more