#[non_exhaustive]pub struct Response {
pub status_code: Option<StatusCode>,
pub headers: HeaderMap,
pub version: Version,
pub cookies: CookieJar,
pub body: ResBody,
pub extensions: Extensions,
}Expand description
Represents an HTTP response.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.status_code: Option<StatusCode>The HTTP status code.WebTransportSession
headers: HeaderMapThe HTTP headers.
version: VersionThe HTTP version.
The HTTP cookies.
body: ResBodyThe HTTP body.
extensions: ExtensionsUsed to store extra data derived from the underlying protocol.
Implementations§
Source§impl Response
impl Response
Creates a new blank Response.
Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
pub fn headers_mut(&mut self) -> &mut HeaderMap
Get mutable headers reference.
Sourcepub fn set_headers(&mut self, headers: HeaderMap)
pub fn set_headers(&mut self, headers: HeaderMap)
Sets headers.
Sourcepub fn add_header<N, V>(
&mut self,
name: N,
value: V,
overwrite: bool,
) -> Result<&mut Self>
pub fn add_header<N, V>( &mut self, name: N, value: V, overwrite: bool, ) -> Result<&mut Self>
Modify a header for this response.
When overwrite is set to true, If the header is already present, the value will be replaced.
When overwrite is set to false, The new header is always appended to the request, even if the header already exists.
Sourcepub fn version_mut(&mut self) -> &mut Version
pub fn version_mut(&mut self) -> &mut Version
Get mutable version reference.
Sourcepub fn replace_body(&mut self, body: ResBody) -> ResBody
pub fn replace_body(&mut self, body: ResBody) -> ResBody
Sets body to a new value and returns old value.
Sourcepub fn is_stamped(&mut self) -> bool
pub fn is_stamped(&mut self) -> bool
If returns true, it means this response is ready for write back and the reset handlers should be skipped.
Available on crate feature cookie only.
cookie only.Get cookies reference.
Available on crate feature cookie only.
cookie only.Get mutable cookies reference.
Available on crate feature cookie only.
cookie only.Helper function for get cookie.
Available on crate feature cookie only.
cookie only.Helper function for add cookie.
Available on crate feature cookie only.
cookie only.Helper function for remove cookie.
Removes cookie from this CookieJar. If an original cookie with the same
name as cookie is present in the jar, a removal cookie will be
present in the delta computation. To properly generate the removal
cookie, cookie must contain the same path and domain as the cookie
that was initially set.
A “removal” cookie is a cookie that has the same name as the original cookie but has an empty value, a max-age of 0, and an expiration date far in the past.
Read more about removal cookies.
Sourcepub fn content_type(&self) -> Option<Mime>
pub fn content_type(&self) -> Option<Mime>
Get content type..
§Example
use salvo_core::http::{Response, StatusCode};
let mut res = Response::new();
assert_eq!(None, res.content_type());
res.headers_mut().insert("content-type", "text/plain".parse().unwrap());
assert_eq!(Some(mime::TEXT_PLAIN), res.content_type());Sourcepub fn status_code(&mut self, code: StatusCode) -> &mut Self
pub fn status_code(&mut self, code: StatusCode) -> &mut Self
Sets status code and returns &mut Self.
§Example
use salvo_core::http::StatusCode;
use salvo_core::http::response::Response;
let mut res = Response::new();
res.status_code(StatusCode::OK);Sourcepub fn render<P>(&mut self, scribe: P)where
P: Scribe,
pub fn render<P>(&mut self, scribe: P)where
P: Scribe,
Render content.
§Example
use salvo_core::http::{Response, StatusCode};
let mut res = Response::new();
res.render("hello world");Sourcepub fn stuff<P>(&mut self, code: StatusCode, scribe: P)where
P: Scribe,
pub fn stuff<P>(&mut self, code: StatusCode, scribe: P)where
P: Scribe,
Render content with status code.
Sourcepub async fn send_file<P>(&mut self, path: P, req_headers: &HeaderMap)
pub async fn send_file<P>(&mut self, path: P, req_headers: &HeaderMap)
Attempts to send a file. If file not exists, not found error will occur.
If you want more settings, you can use NamedFile::builder to create a new NamedFileBuilder.
Sourcepub fn write_body(&mut self, data: impl Into<Bytes>) -> Result<()>
pub fn write_body(&mut self, data: impl Into<Bytes>) -> Result<()>
Write bytes data to body. If body is none, a new ResBody will created.
Sourcepub fn stream<S, O, E>(&mut self, stream: S)where
S: Stream<Item = Result<O, E>> + Send + 'static,
O: Into<BytesFrame> + 'static,
E: Into<BoxedError> + 'static,
pub fn stream<S, O, E>(&mut self, stream: S)where
S: Stream<Item = Result<O, E>> + Send + 'static,
O: Into<BytesFrame> + 'static,
E: Into<BoxedError> + 'static,
Set response’s body to stream.
Sourcepub fn channel(&mut self) -> BodySender
pub fn channel(&mut self) -> BodySender
Create a Body stream with an associated sender half.
Useful when wanting to stream chunks from another thread.
§Example
use salvo_core::prelude::*;
#[handler]
async fn hello(res: &mut Response) {
res.add_header("content-type", "text/plain", true).unwrap();
let mut tx = res.channel();
tokio::spawn(async move {
tx.send_data("Hello world").await.unwrap();
});
}Trait Implementations§
Source§impl ResponseExt for Response
Available on crate feature test only.
impl ResponseExt for Response
test only.Source§async fn take_string(&mut self) -> Result<String>
async fn take_string(&mut self) -> Result<String>
String from response.Source§async fn take_json<T: DeserializeOwned>(&mut self) -> Result<T>
async fn take_json<T: DeserializeOwned>(&mut self) -> Result<T>
T instance.