Skip to main content

Response

Struct Response 

Source
pub struct Response(/* private fields */);
Expand description

An HTTP response. Anything a handler returns becomes one, and middleware get one from next.run(request).

use laravel::http::{Response, StatusCode};

fn gone() -> Response {
    Response::new("This page is gone.").with_status(StatusCode::GONE)
}

Response dereferences to the http crate’s response type, which Axum also uses, so its methods such as headers_mut(), status(), and extensions() work here too.

Implementations§

Source§

impl Response

Source

pub fn new(response: impl IntoResponse) -> Response

A response from anything that implements IntoResponse, such as a string, a (StatusCode, body) tuple, or a view.

Source

pub const fn from_http(response: Response<Body>) -> Response

Wrap a response from the http crate, the type that Axum and tower use.

Source

pub fn into_http(self) -> Response<Body>

Unwrap into the http crate’s response, the type that Axum and tower use.

Send a cookie with this response. A (name, value) pair uses the default attributes, and a Cookie sets its own. The cookie replaces any cookie that this response already sends with the same name, path, and domain.

use laravel::http::{Redirect, Response};

let response = Response::new("Saved").with_cookie(("theme", "dark"));
let redirect = Redirect::to("/").with_cookie(("theme", "dark"));

Remove a browser cookie by sending an expired cookie. A name uses the default path and domain. To match a custom path or domain, pass a Cookie. The cookie values that the current request returns stay unchanged.

use laravel::http::Response;
use laravel::cookie::Cookie;

let response = Response::new("Signed out")
    .without_cookie("remember")
    .without_cookie(Cookie::new("tour", "").path("/admin"));
Source

pub fn with_header<K, V>(self, name: K, value: V) -> Response

Send a header with the response, replacing any header with the same name, including one the response would otherwise send:

use laravel::view::{View, view};

async fn report() -> View {
    view("reports.show", ()).with_header("cache-control", "no-store")
}

A name or value that isn’t a valid header turns the response into a 500 error when the response is sent.

Source

pub fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> Response

Send several headers with the response, such as .with_headers([("x-one", "1"), ("x-two", "2")]). Each header replaces any header with the same name, so when the list holds a name twice, the last value wins.

A name or value that isn’t a valid header turns the response into a 500 error when the response is sent.

Source

pub fn with_status(self, status: impl IntoStatusCode) -> Response

Respond with another status, such as 201 or StatusCode::CREATED.

§Panics

Panics when the number isn’t a valid status code.

Source

pub fn with(self, key: impl Into<String>, value: impl Serialize) -> Response

Flash data to the session for the next request, as Redirect::with does. Use it for a response that leads to another page without being a redirect.

Flashing needs the session that the web group starts. On other routes, the response drops the data and logs a warning.

Methods from Deref<Target = Response<Body>>§

Source

pub fn status(&self) -> StatusCode

Returns the StatusCode.

§Examples
let response: Response<()> = Response::default();
assert_eq!(response.status(), StatusCode::OK);
Source

pub fn status_mut(&mut self) -> &mut StatusCode

Returns a mutable reference to the associated StatusCode.

§Examples
let mut response: Response<()> = Response::default();
*response.status_mut() = StatusCode::CREATED;
assert_eq!(response.status(), StatusCode::CREATED);
Source

pub fn version(&self) -> Version

Returns a reference to the associated version.

§Examples
let response: Response<()> = Response::default();
assert_eq!(response.version(), Version::HTTP_11);
Source

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

Returns a mutable reference to the associated version.

§Examples
let mut response: Response<()> = Response::default();
*response.version_mut() = Version::HTTP_2;
assert_eq!(response.version(), Version::HTTP_2);
Source

pub fn headers(&self) -> &HeaderMap

Returns a reference to the associated header field map.

§Examples
let response: Response<()> = Response::default();
assert!(response.headers().is_empty());
Source

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

Returns a mutable reference to the associated header field map.

§Examples
let mut response: Response<()> = Response::default();
response.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!response.headers().is_empty());
Source

pub fn extensions(&self) -> &Extensions

Returns a reference to the associated extensions.

§Examples
let response: Response<()> = Response::default();
assert!(response.extensions().get::<i32>().is_none());
Source

pub fn extensions_mut(&mut self) -> &mut Extensions

Returns a mutable reference to the associated extensions.

§Examples
let mut response: Response<()> = Response::default();
response.extensions_mut().insert("hello");
assert_eq!(response.extensions().get(), Some(&"hello"));
Source

pub fn body(&self) -> &T

Returns a reference to the associated HTTP body.

§Examples
let response: Response<String> = Response::default();
assert!(response.body().is_empty());
Source

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

Returns a mutable reference to the associated HTTP body.

§Examples
let mut response: Response<String> = Response::default();
response.body_mut().push_str("hello world");
assert!(!response.body().is_empty());

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 Deref for Response

Source§

type Target = Response<Body>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Response as Deref>::Target

Dereferences the value.
Source§

impl DerefMut for Response

Source§

fn deref_mut(&mut self) -> &mut <Response as Deref>::Target

Mutably dereferences the value.
Source§

impl IntoResponse for Response

Source§

fn into_response(self) -> Response

Turn this into a Response. Read more
Send a cookie with this response, replacing any cookie the response already sends with the same name, path, and domain. Read more
Remove a browser cookie. Pass the cookie’s name, or a Cookie with the path and domain that set it. Read more
Source§

fn with_header<K, V>(self, name: K, value: V) -> Response

Send a header with the response, replacing any header with the same name. A name or value that isn’t a valid header makes the response a 500 error.
Source§

fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> Response

Send several headers with the response, each replacing any header with the same name. A name or value that isn’t a valid header makes the response a 500 error.
Source§

impl IntoResponse for Response

Source§

fn into_response(self) -> Response<Body>

Create a response.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Response

§

impl !Sync for Response

§

impl !UnwindSafe for Response

§

impl Forget for Response

§

impl Freeze for Response
where NonZero<u16>: Freeze, *const [Pos] is !null: Freeze, PhantomData<[Pos]>: Freeze, *const u8 is !null: Freeze, PhantomData<Bucket<HeaderValue>>: Freeze, PhantomData<ExtraValue<HeaderValue>>: Freeze, *const HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>> is !null: Freeze, PhantomData<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: Freeze, *const dyn Body<Error = Error, Data = Bytes> + Send is !null: Freeze, PhantomData<dyn Body<Error = Error, Data = Bytes> + Send>: Freeze,

§

impl Move for Response

§

impl Send for Response
where NonZero<u16>: Send, BoxRaw<[Pos]>: Send, RawVecInner: Send, Bytes: Send, BoxRaw<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: Send, BoxRaw<dyn Body<Error = Error, Data = Bytes> + Send>: Send,

§

impl Unpin for Response
where NonZero<u16>: Unpin, Box<[Pos]>: Unpin, *const u8 is !null: Unpin, *const u8: Unpin, Align8<*mut ()>: Unpin, &'static Vtable: Unpin, Box<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: Unpin, Box<dyn Body<Error = Error, Data = Bytes> + Send>: Unpin,

§

impl UnsafeUnpin for Response
where NonZeroU16Inner: UnsafeUnpin, *const [Pos] is !null: UnsafeUnpin, PhantomData<[Pos]>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<Bucket<HeaderValue>>: UnsafeUnpin, PhantomData<ExtraValue<HeaderValue>>: UnsafeUnpin, *const HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>> is !null: UnsafeUnpin, PhantomData<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: UnsafeUnpin, *const dyn Body<Error = Error, Data = Bytes> + Send is !null: UnsafeUnpin, PhantomData<dyn Body<Error = Error, Data = Bytes> + Send>: UnsafeUnpin,

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<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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: RngExt + ?Sized, Self: FakeBase<U>,

Source§

impl<T> FillValue<Option<T>> for T
where T: Send + 'static,

Source§

async fn fill(self) -> Result<Option<T>, Error>

Resolves the column’s value. Read more
Source§

impl<T> FillValue<T> for T
where T: Send + 'static,

Source§

async fn fill(self) -> Result<T, Error>

Resolves the column’s value. Read more
Source§

impl<K> ForeignKey<K> for K

Source§

fn key(&self) -> Option<&K>

Returns the key, or None when the field holds no key.
Source§

fn __from_key(key: K) -> K

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T, N> NotifiedBy<T> for N
where T: Notification<N>,

Source§

fn __routes(&self) -> Vec<Route>

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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<T, V> UpdateValue<T> for V
where V: FillValue<T>,

Source§

fn __update<M, U>(self, column: Column<M, T>, update: U) -> U
where M: Model, U: Updates<M>, T: IntoExpr + Clone + Send + Sync + 'static,

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