pub struct HttpResponseBuilder(/* private fields */);Expand description
A builder for constructing HttpResponse objects.
This struct provides a convenient way to create HTTP responses with customizable status codes, headers, and bodies.
§Examples
use ic_http_types::{HttpResponseBuilder};
use serde_bytes::ByteBuf;
let response = HttpResponseBuilder::ok()
.header("Content-Type", "application/json")
.body("response body")
.build();
assert_eq!(response.status_code, 200);
assert_eq!(response.headers, vec![("Content-Type".to_string(), "application/json".to_string())]);
assert_eq!(response.body, ByteBuf::from("response body"));use ic_http_types::{HttpResponseBuilder};
use serde_bytes::ByteBuf;
let response = HttpResponseBuilder::server_error("internal error")
.header("Retry-After", "120")
.build();
assert_eq!(response.status_code, 500);
assert_eq!(response.headers, vec![("Retry-After".to_string(), "120".to_string())]);
assert_eq!(response.body, ByteBuf::from("internal error"));Implementations§
Source§impl HttpResponseBuilder
impl HttpResponseBuilder
Sourcepub fn bad_request() -> Self
pub fn bad_request() -> Self
Creates a new HttpResponse with a 400 Bad Request status.
Sourcepub fn server_error(reason: impl ToString) -> Self
pub fn server_error(reason: impl ToString) -> Self
Creates a new HttpResponse with a 500 Internal Server Error status.
§Parameters
reason: A string describing the reason for the server error.
Sourcepub fn header(self, name: impl ToString, value: impl ToString) -> Self
pub fn header(self, name: impl ToString, value: impl ToString) -> Self
Adds a header to the HttpResponse.
§Parameters
name: The name of the header.value: The value of the header.
Sourcepub fn with_body_and_content_length(self, bytes: impl Into<Vec<u8>>) -> Self
pub fn with_body_and_content_length(self, bytes: impl Into<Vec<u8>>) -> Self
Sets the body of the HttpResponse and adds a Content-Length header.
§Parameters
bytes: The body content as a byte array.
Sourcepub fn build(self) -> HttpResponse
pub fn build(self) -> HttpResponse
Finalizes the builder and returns the constructed HttpResponse.
Auto Trait Implementations§
impl Freeze for HttpResponseBuilder
impl RefUnwindSafe for HttpResponseBuilder
impl Send for HttpResponseBuilder
impl Sync for HttpResponseBuilder
impl Unpin for HttpResponseBuilder
impl UnwindSafe for HttpResponseBuilder
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