Struct HttpResponse

Source
pub struct HttpResponse<'a> { /* private fields */ }
Expand description

A Candid-encodable representation of an HTTP response. This struct is used by the http_request method of the HTTP Gateway Protocol’s Candid interface.

§Examples

use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .with_body(b"Hello, World!")
    .with_upgrade(false)
    .build();

assert_eq!(response.status_code(), StatusCode::OK);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");
assert_eq!(response.upgrade(), Some(false));

§Helpers

There are also a number of convenience methods for quickly creating an HttpResponse with commonly used HTTP status codes:

use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::ok(b"Hello, World!", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::OK);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");

Implementations§

Source§

impl<'a> HttpResponse<'a>

Source

pub fn ok( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with an OK status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::ok(b"Hello, World!", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::OK);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");
Source

pub fn created( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a CREATED status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::created(b"Hello, World!", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::CREATED);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");
Source

pub fn no_content(headers: Vec<(String, String)>) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a NO_CONTENT status code and the given headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::no_content(vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::NO_CONTENT);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
Source

pub fn moved_permanently( location: impl Into<String>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a MOVED_PERMANENTLY status code and the given location and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::moved_permanently("https://www.example.com", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::MOVED_PERMANENTLY);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into()), ("Location".into(), "https://www.example.com".into())]);
Source

pub fn not_modified(headers: Vec<(String, String)>) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a NOT_MODIFIED status code and the given headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::not_modified(vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::NOT_MODIFIED);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
Source

pub fn temporary_redirect( location: impl Into<String>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a TEMPORARY_REDIRECT status code and the given location and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::temporary_redirect("https://www.example.com", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::TEMPORARY_REDIRECT);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into()), ("Location".into(), "https://www.example.com".into())]);
Source

pub fn bad_request( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a BAD_REQUEST status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::bad_request(b"Bad Request", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::BAD_REQUEST);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Bad Request");
Source

pub fn unauthorized( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with an UNAUTHORIZED status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::unauthorized(b"Unauthorized", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::UNAUTHORIZED);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Unauthorized");
Source

pub fn forbidden( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a FORBIDDEN status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::forbidden(b"Forbidden", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::FORBIDDEN);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Forbidden");
Source

pub fn not_found( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a NOT_FOUND status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};  

let response = HttpResponse::not_found(b"Not Found", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::NOT_FOUND);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Not Found");
Source

pub fn method_not_allowed( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a METHOD_NOT_ALLOWED status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::method_not_allowed(b"Method Not Allowed", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Method Not Allowed");
Source

pub fn too_many_requests( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a CONFLICT status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::too_many_requests(b"Too many requests", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::TOO_MANY_REQUESTS);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Too many requests");
Source

pub fn internal_server_error( body: impl Into<Cow<'a, [u8]>>, headers: Vec<(String, String)>, ) -> HttpResponseBuilder<'a>

Creates a new HttpResponseBuilder initialized with a INTERNAL_SERVER_ERROR status code and the given body and headers.

This method returns an instance of HttpResponseBuilder that can be used to to create an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::internal_server_error(b"Internal Server Error", vec![("Content-Type".into(), "text/plain".into())]).build();

assert_eq!(response.status_code(), StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Internal Server Error");
Source

pub fn builder() -> HttpResponseBuilder<'a>

Creates and returns an instance of HttpResponseBuilder, a builder-style object that can be used to construct an HttpResponse.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .with_body(b"Hello, World!")
    .with_upgrade(false)
    .build();

assert_eq!(response.status_code(), StatusCode::OK);
assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
assert_eq!(response.body(), b"Hello, World!");
assert_eq!(response.upgrade(), Some(false));
Source

pub fn status_code(&self) -> StatusCode

Returns the HTTP status code of the response.

§Examples
use ic_http_certification::{HttpResponse, StatusCode};

let response = HttpResponse::builder()
    .with_status_code(StatusCode::OK)
    .build();

assert_eq!(response.status_code(), StatusCode::OK);
Source

pub fn headers(&self) -> &[HeaderField]

Returns the HTTP headers of the response.

§Examples
use ic_http_certification::HttpResponse;

let response = HttpResponse::builder()
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .build();

assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into())]);
Source

pub fn headers_mut(&mut self) -> &mut Vec<HeaderField>

Returns a mutable reference to the HTTP headers of the response.

§Examples
use ic_http_certification::HttpResponse;

let mut response = HttpResponse::builder()
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .build();

response.headers_mut().push(("Content-Length".into(), "13".into()));

assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into()), ("Content-Length".into(), "13".into())]);
Source

pub fn add_header(&mut self, header: HeaderField)

Adds an additional header to the HTTP response.

§Examples
use ic_http_certification::HttpResponse;

let mut response = HttpResponse::builder()
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .build();

response.add_header(("Content-Length".into(), "13".into()));

assert_eq!(response.headers(), &[("Content-Type".into(), "text/plain".into()), ("Content-Length".into(), "13".into())]);
Source

pub fn body(&self) -> &[u8]

Returns the HTTP body of the response.

§Examples
use ic_http_certification::HttpResponse;

let response = HttpResponse::builder()
    .with_body(b"Hello, World!")
    .build();

assert_eq!(response.body(), b"Hello, World!");
Source

pub fn upgrade(&self) -> Option<bool>

Returns the upgrade flag of the response. This will determine if the HTTP Gateway will upgrade the request to an update call.

§Examples
use ic_http_certification::HttpResponse;

let response = HttpResponse::builder()
    .with_upgrade(true)
    .build();

assert_eq!(response.upgrade(), Some(true));

Trait Implementations§

Source§

impl<'a> CandidType for HttpResponse<'a>

Source§

fn _ty() -> Type

Source§

fn id() -> TypeId

Source§

fn idl_serialize<__S>(&self, __serializer: __S) -> Result<(), __S::Error>
where __S: Serializer,

Source§

fn ty() -> Type

Source§

impl<'a> Clone for HttpResponse<'a>

Source§

fn clone(&self) -> HttpResponse<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HttpResponse<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, 'a> Deserialize<'de> for HttpResponse<'a>

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a> From<HttpResponse<'a>> for HttpResponseBuilder<'a>

Source§

fn from(response: HttpResponse<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<HttpResponse<'a>> for HttpUpdateResponse<'a>

Source§

fn from(response: HttpResponse<'a>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for HttpResponse<'_>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'a> Freeze for HttpResponse<'a>

§

impl<'a> RefUnwindSafe for HttpResponse<'a>

§

impl<'a> Send for HttpResponse<'a>

§

impl<'a> Sync for HttpResponse<'a>

§

impl<'a> Unpin for HttpResponse<'a>

§

impl<'a> UnwindSafe for HttpResponse<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,