#[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§
Source§impl ResponseExt for Response
impl ResponseExt for Response
Source§async fn take_json<T>(&mut self) -> Result<T, Error>where
T: DeserializeOwned,
async fn take_json<T>(&mut self) -> Result<T, Error>where
T: DeserializeOwned,
T instance.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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);Source§impl<T> Pointable for T
impl<T> Pointable for T
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 more