Struct salvo::prelude::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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional 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.

§cookies: CookieJar

The HTTP cookies.

§body: ResBody

The HTTP body.

§extensions: Extensions

Used to store extra data derived from the underlying protocol.

Implementations§

source§

impl Response

source

pub fn new() -> Response

Creates a new blank Response.

source

pub fn with_cookies(cookies: CookieJar) -> Response

Creates a new blank Response.

source

pub fn headers(&self) -> &HeaderMap

Get headers reference.

source

pub fn headers_mut(&mut self) -> &mut HeaderMap

Get mutable headers reference.

source

pub fn set_headers(&mut self, headers: HeaderMap)

Sets headers.

source

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.

source

pub fn version(&self) -> Version

Get version.

source

pub fn version_mut(&mut self) -> &mut Version

Get mutable version reference.

source

pub fn body_mut(&mut self) -> &mut ResBody

Get mutable body reference.

source

pub fn body(&mut self, body: ResBody) -> &mut Response

Sets body.

source

pub fn replace_body(&mut self, body: ResBody) -> ResBody

Sets body to a new value and returns old value.

source

pub fn take_body(&mut self) -> ResBody

Take body from response.

source

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.

source

pub fn cookies(&self) -> &CookieJar

Get cookies reference.

source

pub fn cookies_mut(&mut self) -> &mut CookieJar

Get mutable cookies reference.

source

pub fn cookie<T>(&self, name: T) -> Option<&Cookie<'static>>
where T: AsRef<str>,

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.

source

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());
source

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);
source

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");
source

pub fn stuff<P>(&mut self, code: StatusCode, scribe: P)
where P: Scribe,

Render content with status code.

source

pub async fn send_file<P>(&mut self, path: P, req_headers: &HeaderMap)
where P: Into<PathBuf> + Send,

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.

source

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.

source

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<Box<dyn Error + Send + Sync>> + 'static,

Set response’s body to stream.

source

pub fn channel(&mut self) -> BodySender

Set response’s body to channel.

Trait Implementations§

source§

impl Debug for Response

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Response

source§

fn default() -> Response

Returns the “default value” for a type. Read more
source§

impl Display for Response

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<B> From<Response<B>> for Response
where B: Into<ResBody>,

source§

fn from(res: Response<B>) -> Response

Converts to this type from the input type.
source§

impl ResponseExt for Response

source§

async fn take_string(&mut self) -> Result<String, Error>

Take body as String from response.
source§

async fn take_json<T>(&mut self) -> Result<T, Error>

Take body as deserialize it to type T instance.
source§

async fn take_string_with_charset( &mut self, content_type: Option<&Mime>, charset: &str, compress: Option<&str> ) -> Result<String, Error>

Take body as String from response with charset.
source§

async fn take_bytes( &mut self, content_type: Option<&Mime> ) -> Result<Bytes, Error>

Take all body bytes. If body is none, it will creates and returns a new Bytes.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

source§

fn implicit( self, class: Class, constructed: bool, tag: u32 ) -> TaggedParser<'a, Implicit, Self, E>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FutureExt for T

source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more