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
impl Response
Sourcepub fn new(response: impl IntoResponse) -> Response
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.
Sourcepub const fn from_http(response: Response<Body>) -> Response
pub const fn from_http(response: Response<Body>) -> Response
Wrap a response from the http crate, the type that Axum and tower use.
Sourcepub fn into_http(self) -> Response<Body>
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"));Sourcepub fn with_header<K, V>(self, name: K, value: V) -> Response
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.
Sourcepub fn with_headers<K, V>(
self,
headers: impl IntoIterator<Item = (K, V)>,
) -> Response
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.
Sourcepub fn with_status(self, status: impl IntoStatusCode) -> Response
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.
Sourcepub fn with(self, key: impl Into<String>, value: impl Serialize) -> Response
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>>§
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Returns the StatusCode.
§Examples
let response: Response<()> = Response::default();
assert_eq!(response.status(), StatusCode::OK);Sourcepub fn status_mut(&mut self) -> &mut StatusCode
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);Sourcepub fn version(&self) -> Version
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);Sourcepub fn version_mut(&mut self) -> &mut Version
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);Sourcepub fn headers(&self) -> &HeaderMap
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());Sourcepub fn headers_mut(&mut self) -> &mut HeaderMap
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());Sourcepub fn extensions(&self) -> &Extensions
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());Sourcepub fn extensions_mut(&mut self) -> &mut Extensions
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"));Trait Implementations§
Source§impl IntoResponse for Response
impl IntoResponse for Response
Source§fn with_header<K, V>(self, name: K, value: V) -> Response
fn with_header<K, V>(self, name: K, value: V) -> Response
500 error.Source§fn with_headers<K, V>(
self,
headers: impl IntoIterator<Item = (K, V)>,
) -> Response
fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> Response
500 error.Source§impl IntoResponse for Response
impl IntoResponse for Response
Source§fn into_response(self) -> Response<Body>
fn into_response(self) -> Response<Body>
Auto Trait Implementations§
impl !RefUnwindSafe for Response
impl !Sync for Response
impl !UnwindSafe for Response
impl Forget for Response
impl Freeze for Responsewhere
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
impl Unpin for Response
impl UnsafeUnpin for Responsewhere
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> 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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
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 more