Skip to main content

HttpResponse

Struct HttpResponse 

pub struct HttpResponse<B = BoxBody> {
    res: Response<B>,
    error: Option<Error>,
}
Expand description

An outgoing response.

Fields§

§res: Response<B>§error: Option<Error>

Implementations§

§

impl HttpResponse

pub fn Continue() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::CONTINUE.

pub fn SwitchingProtocols() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::SWITCHING_PROTOCOLS.

pub fn Processing() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PROCESSING.

pub fn Ok() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::OK.

pub fn Created() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::CREATED.

pub fn Accepted() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::ACCEPTED.

pub fn NonAuthoritativeInformation() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NON_AUTHORITATIVE_INFORMATION.

pub fn NoContent() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NO_CONTENT.

pub fn ResetContent() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::RESET_CONTENT.

pub fn PartialContent() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PARTIAL_CONTENT.

pub fn MultiStatus() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::MULTI_STATUS.

pub fn AlreadyReported() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::ALREADY_REPORTED.

pub fn ImUsed() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::IM_USED.

pub fn MultipleChoices() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::MULTIPLE_CHOICES.

pub fn MovedPermanently() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::MOVED_PERMANENTLY.

pub fn Found() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::FOUND.

pub fn SeeOther() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::SEE_OTHER.

pub fn NotModified() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NOT_MODIFIED.

pub fn UseProxy() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::USE_PROXY.

pub fn TemporaryRedirect() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::TEMPORARY_REDIRECT.

pub fn PermanentRedirect() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PERMANENT_REDIRECT.

pub fn BadRequest() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::BAD_REQUEST.

pub fn Unauthorized() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::UNAUTHORIZED.

pub fn PaymentRequired() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PAYMENT_REQUIRED.

pub fn Forbidden() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::FORBIDDEN.

pub fn NotFound() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NOT_FOUND.

pub fn MethodNotAllowed() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::METHOD_NOT_ALLOWED.

pub fn NotAcceptable() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NOT_ACCEPTABLE.

pub fn ProxyAuthenticationRequired() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PROXY_AUTHENTICATION_REQUIRED.

pub fn RequestTimeout() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::REQUEST_TIMEOUT.

pub fn Conflict() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::CONFLICT.

pub fn Gone() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::GONE.

pub fn LengthRequired() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::LENGTH_REQUIRED.

pub fn PreconditionFailed() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PRECONDITION_FAILED.

pub fn PayloadTooLarge() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PAYLOAD_TOO_LARGE.

pub fn UriTooLong() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::URI_TOO_LONG.

pub fn UnsupportedMediaType() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::UNSUPPORTED_MEDIA_TYPE.

pub fn RangeNotSatisfiable() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::RANGE_NOT_SATISFIABLE.

pub fn ExpectationFailed() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::EXPECTATION_FAILED.

pub fn ImATeapot() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::IM_A_TEAPOT.

pub fn MisdirectedRequest() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::MISDIRECTED_REQUEST.

pub fn UnprocessableEntity() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::UNPROCESSABLE_ENTITY.

pub fn Locked() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::LOCKED.

pub fn FailedDependency() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::FAILED_DEPENDENCY.

pub fn UpgradeRequired() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::UPGRADE_REQUIRED.

pub fn PreconditionRequired() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::PRECONDITION_REQUIRED.

pub fn TooManyRequests() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::TOO_MANY_REQUESTS.

pub fn RequestHeaderFieldsTooLarge() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE.

pub fn UnavailableForLegalReasons() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS.

pub fn InternalServerError() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::INTERNAL_SERVER_ERROR.

pub fn NotImplemented() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NOT_IMPLEMENTED.

pub fn BadGateway() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::BAD_GATEWAY.

pub fn ServiceUnavailable() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::SERVICE_UNAVAILABLE.

pub fn GatewayTimeout() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::GATEWAY_TIMEOUT.

pub fn VersionNotSupported() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::HTTP_VERSION_NOT_SUPPORTED.

pub fn VariantAlsoNegotiates() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::VARIANT_ALSO_NEGOTIATES.

pub fn InsufficientStorage() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::INSUFFICIENT_STORAGE.

pub fn LoopDetected() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::LOOP_DETECTED.

pub fn NotExtended() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NOT_EXTENDED.

pub fn NetworkAuthenticationRequired() -> HttpResponseBuilder ⓘ

Creates a new response builder with the status code StatusCode::NETWORK_AUTHENTICATION_REQUIRED.

§

impl HttpResponse

pub fn new(status: StatusCode) -> HttpResponse

Constructs a response.

pub fn build(status: StatusCode) -> HttpResponseBuilder ⓘ

Constructs a response builder with specific HTTP status.

pub fn from_error(error: impl Into<Error>) -> HttpResponse

Create an error response.

§

impl<B> HttpResponse<B>

pub fn with_body(status: StatusCode, body: B) -> HttpResponse<B>

Constructs a response with body

pub fn head(&self) -> &ResponseHead

Returns a reference to response head.

pub fn head_mut(&mut self) -> &mut ResponseHead

Returns a mutable reference to response head.

pub fn error(&self) -> Option<&Error>

The source error for this response

pub fn status(&self) -> StatusCode

Get the response status code

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

Set the StatusCode for this response

pub fn headers(&self) -> &HeaderMap

Get the headers from the response

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

Get a mutable reference to the headers

pub fn cookies(&self) -> CookieIter<'_>

Get an iterator for the cookies set by this response.

Add a cookie to this response.

§Errors

Returns an error if the cookie results in a malformed Set-Cookie header.

Add a “removal” cookie to the response that matches attributes of given cookie.

This will cause browsers/clients to remove stored cookies with this name.

The Set-Cookie header added to the response will have:

  • name matching given cookie;
  • domain matching given cookie;
  • path matching given cookie;
  • an empty value;
  • a max-age of 0;
  • an expiration date far in the past.

If the cookie you’re trying to remove has an explicit path or domain set, those attributes will need to be included in the cookie passed in here.

§Errors

Returns an error if the given name results in a malformed Set-Cookie header.

Remove all cookies with the given name from this response.

Returns the number of cookies removed.

This method can not cause a browser/client to delete any of its stored cookies. Its only purpose is to delete cookies that were added to this response using add_cookie and add_removal_cookie. Use add_removal_cookie to send a “removal” cookie.

pub fn upgrade(&self) -> bool

Connection upgrade status

pub fn keep_alive(&self) -> bool

Keep-alive status for this connection

pub fn extensions(&self) -> Ref<'_, Extensions>

Returns reference to the response-local data/extensions container.

pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions>

Returns reference to the response-local data/extensions container.

pub fn body(&self) -> &B

Returns a reference to this response’s body.

pub fn set_body<B2>(self, body: B2) -> HttpResponse<B2>

Sets new body.

pub fn into_parts(self) -> (HttpResponse<()>, B)

Returns split head and body.

§Implementation Notes

Due to internal performance optimizations, the first element of the returned tuple is an HttpResponse as well but only contains the head of the response this was called on.

pub fn drop_body(self) -> HttpResponse<()>

Drops body and returns new response.

pub fn map_body<F, B2>(self, f: F) -> HttpResponse<B2>
where F: FnOnce(&mut ResponseHead, B) -> B2,

Map the current body type to another using a closure, returning a new response.

Closure receives the response head and the current body type.

pub fn map_into_left_body<R>(self) -> HttpResponse<EitherBody<B, R>>

Map the current body type B to EitherBody::Left(B).

Useful for middleware which can generate their own responses.

pub fn map_into_right_body<L>(self) -> HttpResponse<EitherBody<L, B>>

Map the current body type B to EitherBody::Right(B).

Useful for middleware which can generate their own responses.

pub fn map_into_boxed_body(self) -> HttpResponse
where B: MessageBody + 'static,

Map the current body to a type-erased BoxBody.

pub fn into_body(self) -> B

Returns the response body, dropping all other parts.

Trait Implementations§

§

impl<B> Debug for HttpResponse<B>
where B: MessageBody,

§

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

Formats the value using the given formatter. Read more
§

impl From<Error> for HttpResponse

§

fn from(err: Error) -> HttpResponse

Converts to this type from the input type.
§

impl From<HttpResponseBuilder> for HttpResponse

§

fn from(builder: HttpResponseBuilder) -> HttpResponse

Converts to this type from the input type.
§

impl<B> From<Response<B>> for HttpResponse<B>

§

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

Converts to this type from the input type.
§

impl<B> From<ServiceResponse<B>> for HttpResponse<B>

§

fn from(res: ServiceResponse<B>) -> HttpResponse<B>

Converts to this type from the input type.
§

impl<B> Responder for HttpResponse<B>
where B: MessageBody + 'static,

§

type Body = B

§

fn respond_to( self, _: &HttpRequest, ) -> HttpResponse<<HttpResponse<B> as Responder>::Body>

Convert self to HttpResponse.
§

fn customize(self) -> CustomizeResponder<Self>
where Self: Sized,

Wraps responder to allow alteration of its response. Read more

Auto Trait Implementations§

§

impl<B = BoxBody> !Freeze for HttpResponse<B>

§

impl<B = BoxBody> !RefUnwindSafe for HttpResponse<B>

§

impl<B = BoxBody> !Send for HttpResponse<B>

§

impl<B = BoxBody> !Sync for HttpResponse<B>

§

impl<B> Unpin for HttpResponse<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for HttpResponse<B>
where B: UnsafeUnpin,

§

impl<B = BoxBody> !UnwindSafe for HttpResponse<B>

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

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

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

fn with_current_context(self) -> WithContext<Self> ⓘ

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

impl<T> Instrument for T

§

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

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

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
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: 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> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> ServiceExt for T

§

fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>
where Self: Sized,

Apply a transformation to the response body. Read more
§

fn decompression(self) -> Decompression<Self>
where Self: Sized,

Decompress response bodies. Read more
§

fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using HTTP status codes. Read more
§

fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using gRPC headers. Read more
§

fn follow_redirects(self) -> FollowRedirect<Self>
where Self: Sized,

Follow redirect resposes using the Standard policy. Read more
Source§

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

Source§

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>,

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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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

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

§

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

§

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

§

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