pub struct Response { /* private fields */ }Expand description
An outgoing HTTP response.
Implementations§
Source§impl Response
impl Response
Sourcepub fn new(status: StatusCode) -> Response
pub fn new(status: StatusCode) -> Response
An empty response with status.
Sourcepub fn no_content() -> Response
pub fn no_content() -> Response
204 No Content.
Sourcepub fn json(value: &impl Serialize) -> Response
pub fn json(value: &impl Serialize) -> Response
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) -> Response
pub fn download(bytes: impl Into<Bytes>, file_name: &str) -> Response
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) -> Response
pub fn with_status(self, status: StatusCode) -> Response
Replace the status.
Sourcepub fn with_header(self, name: &str, value: &str) -> Response
pub fn with_header(self, name: &str, value: &str) -> Response
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) -> Response
pub fn with_added_header(self, name: &str, value: &str) -> Response
Append a header, keeping any existing ones of that name.
Sourcepub fn with_content_type(self, content_type: &str) -> Response
pub fn with_content_type(self, content_type: &str) -> Response
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>(self) -> Result<T, Error>where
T: DeserializeOwned,
pub async fn into_json<T>(self) -> Result<T, Error>where
T: DeserializeOwned,
The body, parsed as JSON.
Trait Implementations§
Source§impl IntoResponse for Response
impl IntoResponse for Response
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Auto Trait Implementations§
impl !Freeze for Response
impl !RefUnwindSafe for Response
impl !Sync for Response
impl !UnwindSafe for Response
impl Send for Response
impl Unpin for Response
impl UnsafeUnpin 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more