pub struct Response { /* private fields */ }Expand description
A response to an outgoing request, with its body already read.
let response = laravel::http::client::get("https://example.com/users/1").await?;
if response.status() == 404 {
return Ok(());
}
let user: User = response.ensure_successful()?.json()?;Implementations§
Source§impl Response
impl Response
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
The response’s status, which compares with numbers, as in response.status() == 404.
Sourcepub fn successful(&self) -> bool
pub fn successful(&self) -> bool
Whether the status is 2xx.
Sourcepub fn redirect(&self) -> bool
pub fn redirect(&self) -> bool
Whether the status is 3xx.
The client follows up to 10 redirects, so a response rarely has a 3xx status.
Sourcepub fn client_error(&self) -> bool
pub fn client_error(&self) -> bool
Whether the status is 4xx.
Sourcepub fn server_error(&self) -> bool
pub fn server_error(&self) -> bool
Whether the status is 5xx.
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
A header’s value, if the response has the header and its value is text.
Sourcepub fn json<T>(&self) -> Result<T, Error>where
T: DeserializeOwned,
pub fn json<T>(&self) -> Result<T, Error>where
T: DeserializeOwned,
The body decoded as JSON, whatever the status.
To treat an error response as an error, call
ensure_successful first.
§Errors
Returns an Error::Decode when the body isn’t JSON of this type. The error’s message
includes the status and the start of the body.
Sourcepub fn ensure_successful(self) -> Result<Response, Error>
pub fn ensure_successful(self) -> Result<Response, Error>
Return this response when it’s successful, or an Error::Status
that holds it when its status is outside 2xx.
§Errors
Fails when the status isn’t 2xx.
Trait Implementations§
Source§impl From<Response> for StreamedResponse
impl From<Response> for StreamedResponse
Source§fn from(response: Response) -> StreamedResponse
fn from(response: Response) -> StreamedResponse
Auto Trait Implementations§
impl !Freeze for Response
impl Forget for Response
impl Move for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync 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 u8: UnsafeUnpin,
Align8<*mut ()>: UnsafeUnpin,
&'static Vtable: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
impl UnwindSafe for Responsewhere
NonZero<u16>: UnwindSafe,
BoxRaw<[Pos]>: UnwindSafe,
RawVecInner: UnwindSafe,
*const u8: UnwindSafe,
Align8<*mut ()>: UnwindSafe,
&'static Vtable: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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