pub struct Response { /* private fields */ }Expand description
An outgoing HTTP response.
Implementations§
Source§impl Response
impl Response
Sourcepub fn new(status: StatusCode) -> Self
pub fn new(status: StatusCode) -> Self
An empty response with status.
Sourcepub fn no_content() -> Self
pub fn no_content() -> Self
204 No Content.
Sourcepub fn json(value: &impl Serialize) -> Self
pub fn json(value: &impl Serialize) -> Self
200 OK with value serialised as JSON.
A value that cannot be serialised becomes a 500 describing the
failure, because there is nowhere else for the error to go — the
signature has to produce a response.
Sourcepub fn download(bytes: impl Into<Bytes>, file_name: &str) -> Self
pub fn download(bytes: impl Into<Bytes>, file_name: &str) -> Self
A response that offers the body as a file download.
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
The status code.
Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Mutable headers.
Sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Response attributes, keyed by type.
Sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Mutable response attributes.
Sourcepub fn is_successful(&self) -> bool
pub fn is_successful(&self) -> bool
Whether the status is 2xx.
Sourcepub fn with_status(self, status: StatusCode) -> Self
pub fn with_status(self, status: StatusCode) -> Self
Replace the status.
Sourcepub fn with_header(self, name: &str, value: &str) -> Self
pub fn with_header(self, name: &str, value: &str) -> Self
Set a header, replacing any existing one of that name.
An invalid name or value is dropped rather than panicking: header content often comes from configuration or user data, and taking down a request for it would be worse than omitting it.
Sourcepub fn with_added_header(self, name: &str, value: &str) -> Self
pub fn with_added_header(self, name: &str, value: &str) -> Self
Append a header, keeping any existing ones of that name.
Sourcepub fn with_content_type(self, content_type: &str) -> Self
pub fn with_content_type(self, content_type: &str) -> Self
Set Content-Type.
Attach a Set-Cookie header. Several cookies may be attached.
Instruct the client to drop a cookie.
Sourcepub async fn into_bytes(self) -> Result<Bytes, Error>
pub async fn into_bytes(self) -> Result<Bytes, Error>
The body, collected into bytes.
Consumes the response, because a streaming body can only be read once.
For a test, into_string is usually what you want.
Sourcepub async fn into_string(self) -> Result<String, Error>
pub async fn into_string(self) -> Result<String, Error>
The body, collected and decoded as UTF-8.
The one an assertion reaches for. Without it, reading a body in a test
means response.into_http().into_body().collect().await — correct for a
server, wrong ergonomics for assert_eq!.
Invalid UTF-8 is replaced rather than refused: a test that is about to print this wants to see what arrived, not an error about it.
Sourcepub async fn into_json<T: DeserializeOwned>(self) -> Result<T, Error>
pub async fn into_json<T: DeserializeOwned>(self) -> Result<T, Error>
The body, parsed as JSON.