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,
}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: booli18n only.Implementations§
Source§impl<T> Response<T>
impl<T> Response<T>
Sourcepub fn new<C>(r: C) -> Selfwhere
C: ResponseCodeTrait,
pub fn new<C>(r: C) -> Selfwhere
C: ResponseCodeTrait,
Create a 200 response from a ResponseCodeTrait, with i18n translation enabled
for the message (feature i18n).
Sourcepub fn new_code(code: StatusCode) -> Self
pub fn new_code(code: StatusCode) -> Self
Create a response from a raw HTTP status code, with an empty message and i18n translation disabled.
Sourcepub fn created<C, S>(r: C, location: S) -> Self
pub fn created<C, S>(r: C, location: S) -> Self
Create a 201 Created response with a Location header pointing to location.
Sourcepub fn no_content() -> Self
pub fn no_content() -> Self
Create a 204 No Content response (empty body, no JSON headers).
Sourcepub fn bad_request<S: Into<String>>(s: S) -> Self
pub fn bad_request<S: Into<String>>(s: S) -> Self
Create a 400 Bad Request response with s as the message.
Sourcepub fn redirect<S: Into<String>>(code: StatusCode, s: S) -> Self
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.
Sourcepub fn builder<F>(self, f: F) -> Selfwhere
F: Fn(&mut HttpResponseBuilder) + 'static,
pub fn builder<F>(self, f: F) -> Selfwhere
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.
Sourcepub fn message<S: Into<String>>(self, s: S) -> Self
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).
Sourcepub fn file(name: String, data: Vec<u8>) -> HttpResponse
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).
Sourcepub fn translate(self) -> Self
Available on crate feature i18n only.
pub fn translate(self) -> Self
i18n only.Enable i18n translation of the message (feature i18n).
Enabled by default for Self::new, disabled for Self::new_code.
Sourcepub fn i18n_message(&self, req: &HttpRequest) -> String
Available on crate feature i18n only.
pub fn i18n_message(&self, req: &HttpRequest) -> String
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.