Struct sfo_http::http_server::ResponseBuilder
source · pub struct ResponseBuilder(_);Expand description
Response Builder
Provides an ergonomic way to chain the creation of a response. This is generally accessed through Response::builder
Example
let mut response = Response::builder(203)
.body("body")
.content_type(mime::HTML)
.header("custom-header", "value")
.build();
assert_eq!(response.take_body().into_string().await.unwrap(), "body");
assert_eq!(response.status(), StatusCode::NonAuthoritativeInformation);
assert_eq!(response["custom-header"], "value");
assert_eq!(response.content_type(), Some(mime::HTML));Implementations§
source§impl ResponseBuilder
impl ResponseBuilder
sourcepub fn header(
self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues
) -> ResponseBuilder
pub fn header( self, key: impl Into<HeaderName>, value: impl ToHeaderValues ) -> ResponseBuilder
Sets a header on the response.
let response = Response::builder(200).header("header-name", "header-value").build();
assert_eq!(response["header-name"], "header-value");sourcepub fn content_type(self, content_type: impl Into<Mime>) -> ResponseBuilder
pub fn content_type(self, content_type: impl Into<Mime>) -> ResponseBuilder
Sets the Content-Type header on the response.
let response = Response::builder(200).content_type(mime::HTML).build();
assert_eq!(response["content-type"], "text/html;charset=utf-8");sourcepub fn body(self, body: impl Into<Body>) -> ResponseBuilder
pub fn body(self, body: impl Into<Body>) -> ResponseBuilder
Sets the body of the response.
let mut response = Response::builder(200).body(json!({ "any": "Into<Body>"})).build();
assert_eq!(response.take_body().into_string().await.unwrap(), "{\"any\":\"Into<Body>\"}");Trait Implementations§
source§impl Debug for ResponseBuilder
impl Debug for ResponseBuilder
Auto Trait Implementations§
impl !RefUnwindSafe for ResponseBuilder
impl Send for ResponseBuilder
impl Sync for ResponseBuilder
impl Unpin for ResponseBuilder
impl !UnwindSafe for ResponseBuilder
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
Mutably borrows from an owned value. Read more