pub struct Response {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Bytes,
/* private fields */
}core and non-WebAssembly only.Expand description
HTTP Response representation
Fields§
§status: StatusCodeThe HTTP status code.
headers: HeaderMapThe response headers.
body: BytesThe response body as raw bytes.
Implementations§
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_if_absent(self, name: &str, value: &str) -> Response
pub fn with_header_if_absent(self, name: &str, value: &str) -> Response
Add a custom header to the response only if it is not already present.
If the header already exists, the existing value is preserved.
Invalid header names or values are silently ignored.
Use try_with_header_if_absent if you need error
reporting.
This is useful in middleware that should not overwrite headers already set by handlers (e.g., handler-specific CSP headers should survive middleware processing).
§Examples
use reinhardt_http::Response;
// Inserts the header when it is not already present
let response = Response::ok().with_header_if_absent("X-Custom", "first");
assert_eq!(
response.headers.get("X-Custom").unwrap().to_str().unwrap(),
"first"
);use reinhardt_http::Response;
// Preserves the existing header value
let response = Response::ok()
.with_header("X-Custom", "original")
.with_header_if_absent("X-Custom", "overwrite-attempt");
assert_eq!(
response.headers.get("X-Custom").unwrap().to_str().unwrap(),
"original"
);use reinhardt_http::Response;
// Invalid header names are silently ignored (no panic)
let response = Response::ok().with_header_if_absent("Invalid Header", "value");
assert!(response.headers.is_empty());Sourcepub fn try_with_header_if_absent(
self,
name: &str,
value: &str,
) -> Result<Response, Error>
pub fn try_with_header_if_absent( self, name: &str, value: &str, ) -> Result<Response, Error>
Add a custom header to the response only if it is not already present, returning an error for invalid header names or values.
If the header already exists, the existing value is preserved and Ok(self)
is returned without modification.
§Errors
Returns an error if the header name or value is invalid.
§Examples
use reinhardt_http::Response;
// Inserts when absent
let response = Response::ok()
.try_with_header_if_absent("X-Custom", "value")
.unwrap();
assert_eq!(
response.headers.get("X-Custom").unwrap().to_str().unwrap(),
"value"
);use reinhardt_http::Response;
// Preserves existing header value
let response = Response::ok()
.try_with_header("X-Custom", "original")
.unwrap()
.try_with_header_if_absent("X-Custom", "overwrite-attempt")
.unwrap();
assert_eq!(
response.headers.get("X-Custom").unwrap().to_str().unwrap(),
"original"
);use reinhardt_http::Response;
// Invalid header names return an error
let result = Response::ok().try_with_header_if_absent("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 append_header(self, name: &str, value: &str) -> Response
pub fn append_header(self, name: &str, value: &str) -> Response
Append a header value without replacing existing values.
Unlike with_header which replaces any existing value
for the same header name, this method adds the value alongside existing ones.
Required for headers like Set-Cookie where multiple values must coexist
as separate header lines (RFC 6265 Section 4.1).
§Examples
use reinhardt_http::Response;
let response = Response::ok()
.append_header("Set-Cookie", "a=1; Path=/")
.append_header("Set-Cookie", "b=2; Path=/");
let cookies: Vec<_> = response.headers.get_all("set-cookie").iter().collect();
assert_eq!(cookies.len(), 2);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§
Source§impl IntoResponse for Response
impl IntoResponse for Response
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
impl Eq for Response
impl StructuralPartialEq for Response
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<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
Source§fn should_contain_message(&self, expected: &str)where
E: Display,
fn should_contain_message(&self, expected: &str)where
E: Display,
Source§fn should_have_message(&self, expected: &str)where
E: Display,
fn should_have_message(&self, expected: &str)where
E: Display,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.