pub struct Response {
pub cookies: CookieJar,
/* private fields */
}Fields§
Implementations§
source§impl Response
impl Response
pub fn new(body: impl Into<Bytes>) -> Response
Add cookie
Removes cookie from this CookieJar.
Read more about removal cookies.
sourcepub fn header<K, V>(&mut self, name: K, value: V) -> &mut Response
pub fn header<K, V>(&mut self, name: K, value: V) -> &mut Response
Appends a header to this response .
This function will append the provided key/value as a header to the
internal HeaderMap being constructed. Essentially this is equivalent
to calling HeaderMap::append.
§Examples
let response = Response::default()
.header("Content-Type", "text/html")
.header("X-Custom-Foo", "bar")
.header("content-length", 0)
.body("");sourcepub fn header_typed<H>(&mut self, h: H) -> &mut Responsewhere
H: Header,
pub fn header_typed<H>(&mut self, h: H) -> &mut Responsewhere
H: Header,
Inserts a typed header to this response.
sourcepub fn status<T>(&mut self, status: T) -> &mut Responsewhere
T: TryInto<StatusCode>,
pub fn status<T>(&mut self, status: T) -> &mut Responsewhere
T: TryInto<StatusCode>,
Sets the HTTP status for this response. By default this is StatusCode::OK.
sourcepub fn redirect<T>(&mut self, status: T, url: &str) -> &mut Responsewhere
T: TryInto<StatusCode>,
pub fn redirect<T>(&mut self, status: T, url: &str) -> &mut Responsewhere
T: TryInto<StatusCode>,
The response redirects to the specified URL. [mdn]: https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect
sourcepub fn location(&mut self, location: &str) -> &mut Response
pub fn location(&mut self, location: &str) -> &mut Response
The [Content-Location][mdn] header indicates an alternate location for the returned data.
[mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Location
sourcepub fn content_type(&mut self, content_type: &str) -> &mut Response
pub fn content_type(&mut self, content_type: &str) -> &mut Response
The response with the specified [Content-Type][mdn].
[mdn]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type
sourcepub fn content_type_str(&self) -> Result<&str, Box<dyn Error + Sync + Send>>
pub fn content_type_str(&self) -> Result<&str, Box<dyn Error + Sync + Send>>
Get content type. return Result<&str, Error>
sourcepub fn json<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn json<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/json; charset=utf-8 media type.
sourcepub fn json_pretty<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn json_pretty<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/json; charset=utf-8 media type.
sourcepub fn form<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn form<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/x-www-form-urlencoded; charset=utf-8 media type.
sourcepub fn yaml<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn yaml<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/yaml; charset=utf-8 media type.
sourcepub fn toml<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn toml<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/toml; charset=utf-8 media type.
sourcepub fn xml<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn xml<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/xml; charset=utf-8 media type.
sourcepub fn msgpack<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn msgpack<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/msgpack; charset=utf-8 media type.
sourcepub fn cbor<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
pub fn cbor<T>(&mut self, value: &T) -> &mut Responsewhere
T: Serialize,
The response with application/cbor; charset=utf-8 media type.
sourcepub fn render<R>(self, responder: R) -> Responsewhere
R: Responder,
pub fn render<R>(self, responder: R) -> Responsewhere
R: Responder,
Render content.
§Example
use hypers_core::prelude::Response;
let mut res = Response::default();
res.render("hello world");sourcepub async fn file<P>(
&mut self,
file_path: P,
req_headers: &HeaderMap,
) -> &mut Response
pub async fn file<P>( &mut self, file_path: P, req_headers: &HeaderMap, ) -> &mut Response
file writes the specified file into the body stream in an efficient way. If file not exists, not found error will occur.
sourcepub fn download(&mut self, file_path: impl AsRef<Path>) -> &mut Response
pub fn download(&mut self, file_path: impl AsRef<Path>) -> &mut Response
download writes the specified file into the body stream in an efficient way. On the client side, the file will typically be downloaded with the given filename.
sourcepub async fn payload(&mut self) -> Result<Bytes, Box<dyn Error + Sync + Send>>
pub async fn payload(&mut self) -> Result<Bytes, Box<dyn Error + Sync + Send>>
Get Bytes from response payload
sourcepub fn into_hyper(self) -> Result<Response<Body>, InvalidHeaderValue>
pub fn into_hyper(self) -> Result<Response<Body>, InvalidHeaderValue>
Convert to hyper response.
pub fn map<F>(self, f: F) -> Response
source§impl Response
impl Response
sourcepub fn assert_status<T>(&mut self, status: T)where
T: TryInto<StatusCode>,
pub fn assert_status<T>(&mut self, status: T)where
T: TryInto<StatusCode>,
Asserts that the status code is equals to status.
sourcepub fn assert_status_is_ok(&mut self)
pub fn assert_status_is_ok(&mut self)
Asserts that the status code is 200 OK.
sourcepub fn assert_header<K, V>(&self, key: K, value: V)
pub fn assert_header<K, V>(&self, key: K, value: V)
Asserts that header key is equals to value.
sourcepub fn assert_content_type(&self, content_type: &str)
pub fn assert_content_type(&self, content_type: &str)
Asserts that content type is equals to content_type.
sourcepub async fn assert_body(&mut self, bytes: impl Into<Bytes>)
pub async fn assert_body(&mut self, bytes: impl Into<Bytes>)
Asserts that the response body is bytes::Bytes and it equals to bytes::Bytes.
sourcepub async fn assert_json(&mut self, json: &impl Serialize)
pub async fn assert_json(&mut self, json: &impl Serialize)
Asserts that the response body is JSON and it equals to json.
sourcepub async fn assert_xml(&mut self, xml: &impl Serialize)
pub async fn assert_xml(&mut self, xml: &impl Serialize)
Asserts that the response body is XML and it equals to xml.
sourcepub async fn assert_yaml(&mut self, yaml: &impl Serialize)
pub async fn assert_yaml(&mut self, yaml: &impl Serialize)
Asserts that the response body is yaml and it equals to yaml.
pub async fn take_string( &mut self, ) -> Result<String, Box<dyn Error + Sync + Send>>
pub async fn take_json<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>where
T: DeserializeOwned,
pub async fn take_form<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>where
T: DeserializeOwned,
pub async fn take_yaml<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>where
T: DeserializeOwned,
pub async fn take_toml<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>where
T: DeserializeOwned,
pub async fn take_xml<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>where
T: DeserializeOwned,
pub async fn take_msgpack<T>(
&mut self,
) -> Result<T, Box<dyn Error + Sync + Send>>where
T: DeserializeOwned,
pub async fn take_cbor<T>(&mut self) -> Result<T, Box<dyn Error + Sync + Send>>where
T: DeserializeOwned,
Methods from Deref<Target = Response<Body>>§
sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the StatusCode.
§Examples
let response: Response<()> = Response::default();
assert_eq!(response.status(), StatusCode::OK);sourcepub fn status_mut(&mut self) -> &mut StatusCode
pub fn status_mut(&mut self) -> &mut StatusCode
Returns a mutable reference to the associated StatusCode.
§Examples
let mut response: Response<()> = Response::default();
*response.status_mut() = StatusCode::CREATED;
assert_eq!(response.status(), StatusCode::CREATED);sourcepub fn version(&self) -> Version
pub fn version(&self) -> Version
Returns a reference to the associated version.
§Examples
let response: Response<()> = Response::default();
assert_eq!(response.version(), Version::HTTP_11);sourcepub fn version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
Returns a mutable reference to the associated version.
§Examples
let mut response: Response<()> = Response::default();
*response.version_mut() = Version::HTTP_2;
assert_eq!(response.version(), Version::HTTP_2);sourcepub fn headers(&self) -> &HeaderMap
pub fn headers(&self) -> &HeaderMap
Returns a reference to the associated header field map.
§Examples
let response: Response<()> = Response::default();
assert!(response.headers().is_empty());sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Returns a mutable reference to the associated header field map.
§Examples
let mut response: Response<()> = Response::default();
response.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!response.headers().is_empty());sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Returns a reference to the associated extensions.
§Examples
let response: Response<()> = Response::default();
assert!(response.extensions().get::<i32>().is_none());sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Returns a mutable reference to the associated extensions.
§Examples
let mut response: Response<()> = Response::default();
response.extensions_mut().insert("hello");
assert_eq!(response.extensions().get(), Some(&"hello"));