Skip to main content

Response

Struct Response 

Source
pub struct Response<T> {
    pub http_code: StatusCode,
    pub code: i64,
    pub message: String,
    pub data: Option<T>,
    pub builder: Vec<ResponseBuilderFn>,
    pub translate: bool,
}
Available on crate feature response only.
Expand description

Uniform response type with business code/message/data and i18n support.

Build one from a ResponseCodeTrait (via Self::new) or straight from an HTTP StatusCode (via Self::new_code); finalize it through the builder-style methods (message, data, builder, …). Response<serde_json::Value> (aka JsonResponse, feature response-json) implements Responder and serializes the body as JSON.

Fields§

§http_code: StatusCode§code: i64§message: String§data: Option<T>§builder: Vec<ResponseBuilderFn>§translate: bool
Available on crate feature i18n only.

Implementations§

Source§

impl<T> Response<T>

Source

pub fn new<C>(r: C) -> Self

Create a 200 response from a ResponseCodeTrait, with i18n translation enabled for the message (feature i18n).

Source

pub fn new_code(code: StatusCode) -> Self

Create a response from a raw HTTP status code, with an empty message and i18n translation disabled.

Source

pub fn created<C, S>(r: C, location: S) -> Self

Create a 201 Created response with a Location header pointing to location.

Source

pub fn no_content() -> Self

Create a 204 No Content response (empty body, no JSON headers).

Source

pub fn bad_request<S: Into<String>>(s: S) -> Self

Create a 400 Bad Request response with s as the message.

Source

pub fn forbidden() -> Self

Create a 403 Forbidden response.

Source

pub fn not_found() -> Self

Create a 404 Not Found response.

Source

pub fn redirect<S: Into<String>>(code: StatusCode, s: S) -> Self

Create a redirect response (code should be a 3xx status) with a Location header pointing to s.

Source

pub fn builder<F>(self, f: F) -> Self
where F: Fn(&mut HttpResponseBuilder) + 'static,

Add a callback to mutate the HttpResponseBuilder before the response is built (e.g. to insert extra headers). Multiple callbacks are applied in call order.

Source

pub fn message<S: Into<String>>(self, s: S) -> Self

Override the response message. With feature i18n the message is treated as a translation key (see Self::translate).

Source

pub fn data(self, data: T) -> Self

Attach the response payload.

Source

pub fn file(name: String, data: Vec<u8>) -> HttpResponse

Build a file-download response: data is sent as an attachment named name (application/octet-stream).

Source

pub fn translate(self) -> Self

Available on crate feature i18n only.

Enable i18n translation of the message (feature i18n).

Enabled by default for Self::new, disabled for Self::new_code.

Source

pub fn i18n_message(&self, req: &HttpRequest) -> String

Available on crate feature i18n only.

Translate the message with the request locale (from crate::request::Extension, falling back to locale.default in GlobalState).

Returns the message as-is when translation is disabled, when GlobalState is not registered, or when the request extension is missing.

Source§

impl Response<Value>

Source

pub fn json<T: Serialize>(self, data: T) -> Self

Available on crate feature response-json only.

Attach data, serialized to JSON, as the response payload.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Response<T>

§

impl<T> !Send for Response<T>

§

impl<T> !Sync for Response<T>

§

impl<T> !UnwindSafe for Response<T>

§

impl<T> Freeze for Response<T>
where Option<T>: Freeze,

§

impl<T> Unpin for Response<T>
where Option<T>: Unpin,

§

impl<T> UnsafeUnpin for Response<T>
where Option<T>: 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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