pub struct Response { /* private fields */ }
Expand description
An HTTP response
Implementations§
Source§impl Response
impl Response
Sourcepub fn builder<S>(status: S) -> ResponseBuilder
pub fn builder<S>(status: S) -> ResponseBuilder
Begin a chained response builder. For more details, see ResponseBuilder
§Example:
let mut response = Response::builder(203)
.body("<html>hi</html>")
.header("custom-header", "value")
.content_type(mime::HTML)
.build();
assert_eq!(response.take_body().into_string().await.unwrap(), "<html>hi</html>");
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
assert_eq!(response["custom-header"], "value");
assert_eq!(response["content-type"], "text/html;charset=utf-8");
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the http status code.
Sourcepub fn set_status<S>(&mut self, status: S)
pub fn set_status<S>(&mut self, status: S)
Set the http status code.
§Example:
let mut response = Response::new(StatusCode::Ok);
response.set_status(418); // the status can be a valid u16 http status code
assert_eq!(response.status(), StatusCode::ImATeapot);
response.set_status(StatusCode::NonAuthoritativeInformation); // or a tide::StatusCode
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
§Panics
set_status
will panic if the status argument cannot be successfully converted into a StatusCode.
Response::new(200).set_status(210); // this is not an established status code and will panic
Sourcepub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues>
Get an HTTP header.
Sourcepub fn header_mut(
&mut self,
name: impl Into<HeaderName>,
) -> Option<&mut HeaderValues>
pub fn header_mut( &mut self, name: impl Into<HeaderName>, ) -> Option<&mut HeaderValues>
Get an HTTP header mutably.
Sourcepub fn remove_header(
&mut self,
name: impl Into<HeaderName>,
) -> Option<HeaderValues>
pub fn remove_header( &mut self, name: impl Into<HeaderName>, ) -> Option<HeaderValues>
Remove a header.
Sourcepub fn insert_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
)
pub fn insert_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )
Insert an HTTP header.
Sourcepub fn append_header(
&mut self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
)
pub fn append_header( &mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues, )
Append an HTTP header.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_>
pub fn iter_mut(&mut self) -> IterMut<'_>
An iterator visiting all header pairs in arbitrary order, with mutable references to the values.
Sourcepub fn header_names(&self) -> Names<'_>
pub fn header_names(&self) -> Names<'_>
An iterator visiting all header names in arbitrary order.
Sourcepub fn header_values(&self) -> Values<'_>
pub fn header_values(&self) -> Values<'_>
An iterator visiting all header values in arbitrary order.
Sourcepub fn content_type(&self) -> Option<Mime>
pub fn content_type(&self) -> Option<Mime>
Get the response content type as a Mime
.
This gets the request Content-Type
header.
Sourcepub fn set_content_type(&mut self, mime: impl Into<Mime>)
pub fn set_content_type(&mut self, mime: impl Into<Mime>)
Set the response content type from a MIME
.
This sets the response Content-Type
header.
Sourcepub fn take_body(&mut self) -> Body
pub fn take_body(&mut self) -> Body
Take the response body as a Body
.
This method can be called after the body has already been taken or read,
but will return an empty Body
.
Useful for adjusting the whole body, such as in middleware.
Sourcepub fn swap_body(&mut self, body: &mut Body)
pub fn swap_body(&mut self, body: &mut Body)
Swaps the value of the body with another body, without deinitializing either one.
§Examples
use tide::Response;
let mut req = Response::new(200);
req.set_body("Hello, Nori!");
let mut body = "Hello, Chashu!".into();
req.swap_body(&mut body);
let mut string = String::new();
body.read_to_string(&mut string).await?;
assert_eq!(&string, "Hello, Nori!");
Insert cookie in the cookie jar.
Removes the cookie. This instructs the CookiesMiddleware
to send a cookie with empty value
in the response.
§Warning
Take care when calling this function with a cookie that was returned by
Request::cookie
. As per section 5.3 step 11 of RFC 6265, a new
cookie is only treated as the same as an old one if it has a matching name, domain and
path.
The domain and path are not sent to the server on subsequent HTTP requests, so if a cookie was originally set with a domain and/or path, calling this function on a cookie with the same name but with either a different, or no, domain and/or path will lead to us sending an empty cookie that the user agent will treat as unrelated to the original one, and will thus not remove the old one.
To avoid this you can manually set the domain and
path as necessary after retrieving the cookie using
Request::cookie
.
Sourcepub fn error(&self) -> Option<&Error>
pub fn error(&self) -> Option<&Error>
Returns an optional reference to an error if the response contains one.
Sourcepub fn downcast_error<E>(&self) -> Option<&E>
pub fn downcast_error<E>(&self) -> Option<&E>
Returns a reference to the original error associated with this response if there is one and if it can be downcast to the specified type.
§Example
use tide::Response;
let error = std::io::Error::new(ErrorKind::Other, "oh no!");
let error = tide::http::Error::from(error);
let mut res = Response::new(400);
res.set_error(error);
if let Some(err) = res.downcast_error::<std::io::Error>() {
// Do something with the `std::io::Error`.
}
Sourcepub fn take_error(&mut self) -> Option<Error>
pub fn take_error(&mut self) -> Option<Error>
Takes the error from the response if one exists, replacing it with None
.
Sourcepub fn set_error(&mut self, error: impl Into<Error>)
pub fn set_error(&mut self, error: impl Into<Error>)
Sets the response’s error, overwriting any existing error.
This is particularly useful for middleware which would like to notify further middleware that an error has occured without overwriting the existing response.
Sourcepub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>
pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T>
Get a response scoped extension value.
Sourcepub fn insert_ext<T: Send + Sync + 'static>(&mut self, val: T)
pub fn insert_ext<T: Send + Sync + 'static>(&mut self, val: T)
Set a response scoped extension value.
Trait Implementations§
Source§impl From<StatusCode> for Response
impl From<StatusCode> for Response
Source§fn from(status: StatusCode) -> Self
fn from(status: StatusCode) -> Self
Source§impl Index<&str> for Response
impl Index<&str> for Response
Source§fn index(&self, name: &str) -> &HeaderValues
fn index(&self, name: &str) -> &HeaderValues
Returns a reference to the value corresponding to the supplied name.
§Panics
Panics if the name is not present in Response
.
Source§type Output = HeaderValues
type Output = HeaderValues
Source§impl Index<HeaderName> for Response
impl Index<HeaderName> for Response
Source§fn index(&self, name: HeaderName) -> &HeaderValues
fn index(&self, name: HeaderName) -> &HeaderValues
Returns a reference to the value corresponding to the supplied name.
§Panics
Panics if the name is not present in Response
.