pub struct Response {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Bytes,
/* private fields */
}Expand description
HTTP Response representation
Fields§
§status: StatusCode§headers: HeaderMap§body: BytesImplementations§
Source§impl Response
impl Response
Sourcepub fn new(status: StatusCode) -> Response
pub fn new(status: StatusCode) -> Response
Create a new Response with the given status code
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::new(StatusCode::OK);
assert_eq!(response.status, StatusCode::OK);
assert!(response.body.is_empty());Sourcepub fn ok() -> Response
pub fn ok() -> Response
Create a Response with HTTP 200 OK status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::ok();
assert_eq!(response.status, StatusCode::OK);Sourcepub fn created() -> Response
pub fn created() -> Response
Create a Response with HTTP 201 Created status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::created();
assert_eq!(response.status, StatusCode::CREATED);Sourcepub fn no_content() -> Response
pub fn no_content() -> Response
Create a Response with HTTP 204 No Content status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::no_content();
assert_eq!(response.status, StatusCode::NO_CONTENT);Sourcepub fn bad_request() -> Response
pub fn bad_request() -> Response
Create a Response with HTTP 400 Bad Request status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::bad_request();
assert_eq!(response.status, StatusCode::BAD_REQUEST);Create a Response with HTTP 401 Unauthorized status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::unauthorized();
assert_eq!(response.status, StatusCode::UNAUTHORIZED);Sourcepub fn forbidden() -> Response
pub fn forbidden() -> Response
Create a Response with HTTP 403 Forbidden status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::forbidden();
assert_eq!(response.status, StatusCode::FORBIDDEN);Sourcepub fn not_found() -> Response
pub fn not_found() -> Response
Create a Response with HTTP 404 Not Found status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::not_found();
assert_eq!(response.status, StatusCode::NOT_FOUND);Sourcepub fn internal_server_error() -> Response
pub fn internal_server_error() -> Response
Create a Response with HTTP 500 Internal Server Error status
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::internal_server_error();
assert_eq!(response.status, StatusCode::INTERNAL_SERVER_ERROR);Sourcepub fn gone() -> Response
pub fn gone() -> Response
Create a Response with HTTP 410 Gone status
Used when a resource has been permanently removed.
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::gone();
assert_eq!(response.status, StatusCode::GONE);Sourcepub fn permanent_redirect(location: impl AsRef<str>) -> Response
pub fn permanent_redirect(location: impl AsRef<str>) -> Response
Create a Response with HTTP 301 Moved Permanently (permanent redirect)
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::permanent_redirect("/new-location");
assert_eq!(response.status, StatusCode::MOVED_PERMANENTLY);
assert_eq!(
response.headers.get("location").unwrap().to_str().unwrap(),
"/new-location"
);Sourcepub fn temporary_redirect(location: impl AsRef<str>) -> Response
pub fn temporary_redirect(location: impl AsRef<str>) -> Response
Create a Response with HTTP 302 Found (temporary redirect)
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::temporary_redirect("/temp-location");
assert_eq!(response.status, StatusCode::FOUND);
assert_eq!(
response.headers.get("location").unwrap().to_str().unwrap(),
"/temp-location"
);Sourcepub fn temporary_redirect_preserve_method(location: impl AsRef<str>) -> Response
pub fn temporary_redirect_preserve_method(location: impl AsRef<str>) -> Response
Create a Response with HTTP 307 Temporary Redirect (preserves HTTP method)
Unlike 302, this guarantees the request method is preserved during redirect.
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::temporary_redirect_preserve_method("/temp-location");
assert_eq!(response.status, StatusCode::TEMPORARY_REDIRECT);
assert_eq!(
response.headers.get("location").unwrap().to_str().unwrap(),
"/temp-location"
);Sourcepub fn with_body(self, body: impl Into<Bytes>) -> Response
pub fn with_body(self, body: impl Into<Bytes>) -> Response
Set the response body
§Examples
use reinhardt_http::Response;
use bytes::Bytes;
let response = Response::ok().with_body("Hello, World!");
assert_eq!(response.body, Bytes::from("Hello, World!"));Sourcepub fn try_with_header(self, name: &str, value: &str) -> Result<Response, Error>
pub fn try_with_header(self, name: &str, value: &str) -> Result<Response, Error>
Try to add a custom header to the response, returning an error on invalid inputs.
§Errors
Returns Err if the header name or value is invalid according to HTTP specifications.
§Examples
use reinhardt_http::Response;
let response = Response::ok().try_with_header("X-Custom-Header", "custom-value").unwrap();
assert_eq!(
response.headers.get("X-Custom-Header").unwrap().to_str().unwrap(),
"custom-value"
);use reinhardt_http::Response;
// Invalid header names return an error instead of panicking
let result = Response::ok().try_with_header("Invalid Header", "value");
assert!(result.is_err());Sourcepub fn with_header(self, name: &str, value: &str) -> Response
pub fn with_header(self, name: &str, value: &str) -> Response
Add a custom header to the response.
Invalid header names or values are silently ignored.
Use try_with_header if you need error reporting.
§Examples
use reinhardt_http::Response;
let response = Response::ok().with_header("X-Custom-Header", "custom-value");
assert_eq!(
response.headers.get("X-Custom-Header").unwrap().to_str().unwrap(),
"custom-value"
);use reinhardt_http::Response;
// Invalid header names are silently ignored (no panic)
let response = Response::ok().with_header("Invalid Header", "value");
assert!(response.headers.is_empty());Sourcepub fn with_location(self, location: &str) -> Response
pub fn with_location(self, location: &str) -> Response
Add a Location header to the response (typically used for redirects)
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
let response = Response::new(StatusCode::FOUND).with_location("/redirect-target");
assert_eq!(
response.headers.get("location").unwrap().to_str().unwrap(),
"/redirect-target"
);Sourcepub fn with_json<T>(self, data: &T) -> Result<Response, Error>where
T: Serialize,
pub fn with_json<T>(self, data: &T) -> Result<Response, Error>where
T: Serialize,
Set the response body to JSON and add appropriate Content-Type header
§Examples
use reinhardt_http::Response;
use serde_json::json;
let data = json!({"message": "Hello, World!"});
let response = Response::ok().with_json(&data).unwrap();
assert_eq!(
response.headers.get("content-type").unwrap().to_str().unwrap(),
"application/json"
);Sourcepub fn with_typed_header(self, key: HeaderName, value: HeaderValue) -> Response
pub fn with_typed_header(self, key: HeaderName, value: HeaderValue) -> Response
Add a custom header using typed HeaderName and HeaderValue
§Examples
use reinhardt_http::Response;
use hyper::header::{HeaderName, HeaderValue};
let header_name = HeaderName::from_static("x-custom-header");
let header_value = HeaderValue::from_static("custom-value");
let response = Response::ok().with_typed_header(header_name, header_value);
assert_eq!(
response.headers.get("x-custom-header").unwrap().to_str().unwrap(),
"custom-value"
);Sourcepub fn should_stop_chain(&self) -> bool
pub fn should_stop_chain(&self) -> bool
Check if this response should stop the middleware chain
When true, no further middleware or handlers will be executed.
§Examples
use reinhardt_http::Response;
let response = Response::ok();
assert!(!response.should_stop_chain());
let stopping_response = Response::ok().with_stop_chain(true);
assert!(stopping_response.should_stop_chain());Sourcepub fn with_stop_chain(self, stop: bool) -> Response
pub fn with_stop_chain(self, stop: bool) -> Response
Set whether this response should stop the middleware chain
When set to true, the middleware chain will stop processing and return this response immediately, skipping any remaining middleware and handlers.
This is useful for early returns in middleware, such as:
- Authentication failures (401 Unauthorized)
- CORS preflight responses (204 No Content)
- Rate limiting rejections (429 Too Many Requests)
- Cache hits (304 Not Modified)
§Examples
use reinhardt_http::Response;
use hyper::StatusCode;
// Early return for authentication failure
let auth_failure = Response::unauthorized()
.with_body("Authentication required")
.with_stop_chain(true);
assert!(auth_failure.should_stop_chain());
// CORS preflight response
let preflight = Response::no_content()
.with_header("Access-Control-Allow-Origin", "*")
.with_stop_chain(true);
assert!(preflight.should_stop_chain());Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Response
impl RefUnwindSafe for Response
impl Send for Response
impl Sync for Response
impl Unpin for Response
impl UnsafeUnpin for Response
impl UnwindSafe for Response
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
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