pub struct Response<S: ResponseCode> { /* private fields */ }
Expand description
An HTTP response.
Implementations§
Source§impl<S: ResponseCode> Response<S>
impl<S: ResponseCode> Response<S>
Sourcepub fn with_context<Ctx: RequestContext>(code: S, ctx: &Ctx) -> Self
pub fn with_context<Ctx: RequestContext>(code: S, ctx: &Ctx) -> Self
Creates a new instance with the request context.
Sourcepub fn context<Ctx: RequestContext>(self, ctx: &Ctx) -> Self
pub fn context<Ctx: RequestContext>(self, ctx: &Ctx) -> Self
Provides the request context for the response.
Sourcepub fn render<T: Serialize>(self, template_name: &str, data: T) -> Self
Available on crate feature view
only.
pub fn render<T: Serialize>(self, template_name: &str, data: T) -> Self
view
only.Renders a template with the data and sets it as the reponse.
Sourcepub fn set_status_code(&mut self, status_code: impl Into<u16>)
pub fn set_status_code(&mut self, status_code: impl Into<u16>)
Sets the status code.
Sourcepub fn set_error_code(&mut self, error_code: impl Into<S::ErrorCode>)
pub fn set_error_code(&mut self, error_code: impl Into<S::ErrorCode>)
Sets the error code.
Sourcepub fn set_business_code(&mut self, business_code: impl Into<S::BusinessCode>)
pub fn set_business_code(&mut self, business_code: impl Into<S::BusinessCode>)
Sets the bussiness code.
Sourcepub fn set_instance(&mut self, instance: impl Into<SharedString>)
pub fn set_instance(&mut self, instance: impl Into<SharedString>)
Sets a URI reference that identifies the specific occurrence of the problem.
Sourcepub fn set_message(&mut self, message: impl Into<SharedString>)
pub fn set_message(&mut self, message: impl Into<SharedString>)
Sets the message. If the response is not successful, it should be a human-readable explanation specific to this occurrence of the problem.
Sourcepub fn set_error_message(&mut self, error: impl Into<Error>)
pub fn set_error_message(&mut self, error: impl Into<Error>)
Sets the error message.
Sourcepub fn set_json_data(&mut self, data: impl Into<JsonValue>)
pub fn set_json_data(&mut self, data: impl Into<JsonValue>)
Sets the JSON data.
Sourcepub fn set_bytes_data(&mut self, data: impl Into<Bytes>)
pub fn set_bytes_data(&mut self, data: impl Into<Bytes>)
Sets the bytes data.
Sourcepub fn set_validation_data(&mut self, validation: Validation)
pub fn set_validation_data(&mut self, validation: Validation)
Sets the response data for the validation.
Sourcepub fn set_data_transformer(&mut self, transformer: DataTransformer)
pub fn set_data_transformer(&mut self, transformer: DataTransformer)
Sets a transformer for the response data.
Sourcepub fn set_content_type(&mut self, content_type: impl Into<SharedString>)
pub fn set_content_type(&mut self, content_type: impl Into<SharedString>)
Sets the content type.
§Note
Currently, we have built-in support for the following values:
application/json
application/jsonlines
application/octet-stream
application/problem+json
application/x-www-form-urlencoded
text/csv
text/html
text/plain
Sourcepub fn set_form_response(&mut self, data: impl Into<JsonValue>)
pub fn set_form_response(&mut self, data: impl Into<JsonValue>)
Sets the form data as the response body.
Sourcepub fn set_json_response(&mut self, data: impl Into<JsonValue>)
pub fn set_json_response(&mut self, data: impl Into<JsonValue>)
Sets the JSON data as the response body.
Sourcepub fn set_jsonlines_response(&mut self, data: impl Into<JsonValue>)
pub fn set_jsonlines_response(&mut self, data: impl Into<JsonValue>)
Sets the JSON Lines data as the response body.
Sourcepub fn set_csv_response(&mut self, data: impl Into<JsonValue>)
pub fn set_csv_response(&mut self, data: impl Into<JsonValue>)
Sets the CSV data as the response body.
Sourcepub fn set_text_response(&mut self, data: impl Into<String>)
pub fn set_text_response(&mut self, data: impl Into<String>)
Sets the plain text as the response body.
Sourcepub fn set_bytes_response(&mut self, data: impl Into<Bytes>)
pub fn set_bytes_response(&mut self, data: impl Into<Bytes>)
Sets the bytes data as the response body.
Available on crate feature cookie
only.
cookie
only.Sends a cookie to the user agent.
Sourcepub fn record_server_timing(
&mut self,
name: impl Into<SharedString>,
description: impl Into<Option<SharedString>>,
duration: impl Into<Option<Duration>>,
)
pub fn record_server_timing( &mut self, name: impl Into<SharedString>, description: impl Into<Option<SharedString>>, duration: impl Into<Option<Duration>>, )
Records a server timing metric entry.
Sourcepub fn insert_header(
&mut self,
name: impl Into<SharedString>,
value: impl ToString,
)
pub fn insert_header( &mut self, name: impl Into<SharedString>, value: impl ToString, )
Inserts a custom header.
Sourcepub fn get_header(&self, name: &str) -> Option<&str>
pub fn get_header(&self, name: &str) -> Option<&str>
Gets a custome header with the given name.
Sourcepub fn status_code(&self) -> u16
pub fn status_code(&self) -> u16
Returns the status code as u16
.
Sourcepub fn error_code(&self) -> Option<&S::ErrorCode>
pub fn error_code(&self) -> Option<&S::ErrorCode>
Returns the error code.
Sourcepub fn business_code(&self) -> Option<&S::BusinessCode>
pub fn business_code(&self) -> Option<&S::BusinessCode>
Returns the business code.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Returns true
if the response is successful or false
otherwise.
Sourcepub fn has_context(&self) -> bool
pub fn has_context(&self) -> bool
Returns true
if the response has a request context.
Sourcepub fn request_id(&self) -> Uuid
pub fn request_id(&self) -> Uuid
Returns the request ID.
Sourcepub fn content_type(&self) -> &str
pub fn content_type(&self) -> &str
Returns the content type.
Sourcepub fn headers(&self) -> &[(SharedString, String)]
pub fn headers(&self) -> &[(SharedString, String)]
Returns the custom headers.
Sourcepub fn trace_context(&self) -> (String, String)
pub fn trace_context(&self) -> (String, String)
Returns the trace context in the form (traceparent, tracestate)
.
Sourcepub fn server_timing(&self) -> String
pub fn server_timing(&self) -> String
Returns the server timing.
Sourcepub fn read_bytes(&mut self) -> Result<Bytes, Error>
pub fn read_bytes(&mut self) -> Result<Bytes, Error>
Reads the response into a byte buffer.
Sourcepub fn response_time(&self) -> Duration
pub fn response_time(&self) -> Duration
Sourcepub fn finalize(self) -> impl Iterator<Item = (SharedString, String)>
pub fn finalize(self) -> impl Iterator<Item = (SharedString, String)>
Consumes self
and returns the custom headers.
Source§impl Response<StatusCode>
impl Response<StatusCode>
Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
Constructs a new response with status 400 Bad Request
.
Sourcepub fn internal_server_error() -> Self
pub fn internal_server_error() -> Self
Constructs a new response with status 500 Internal Server Error
.