#[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 Response, Error>
pub fn add_header<N, V>( &mut self, name: N, value: V, overwrite: bool, ) -> Result<&mut Response, Error>
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.
Get cookies reference.
Get mutable cookies reference.
Helper function for get cookie.
Helper function for add cookie.
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 Response
pub fn status_code(&mut self, code: StatusCode) -> &mut Response
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<(), Error>
pub fn write_body(&mut self, data: impl Into<Bytes>) -> Result<(), Error>
Write bytes data to body. If body is none, a new ResBody will created.
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§
Auto Trait Implementations§
impl !Freeze for Response
impl !RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl !UnwindSafe for Response
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string() Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString. Read moreSource§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.