Struct salvo_core::http::response::Response
source · #[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: HeaderMap
The HTTP headers.
version: Version
The HTTP version.
The HTTP cookies.
body: ResBody
The HTTP body.
extensions: Extensions
Used 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
Set response’s body to channel.
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.