HttpResponseBuilder

Struct HttpResponseBuilder 

Source
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

Source

pub fn ok() -> Self

Creates a new HttpResponse with a 200 OK status.

Source

pub fn bad_request() -> Self

Creates a new HttpResponse with a 400 Bad Request status.

Source

pub fn not_found() -> Self

Creates a new HttpResponse with a 404 Not Found status.

Source

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.
Source

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.
Source

pub fn body(self, bytes: impl Into<Vec<u8>>) -> Self

Sets the body of the HttpResponse.

§Parameters
  • bytes: The body content as a byte array.
Source

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.
Source

pub fn build(self) -> HttpResponse

Finalizes the builder and returns the constructed HttpResponse.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.