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:
- OK
- CREATED
- NO_CONTENT
- MOVED_PERMANENTLY
- TEMPORARY_REDIRECT
- BAD_REQUEST
- UNAUTHORIZED
- FORBIDDEN
- NOT_FOUND
- METHOD_NOT_ALLOWED
- TOO_MANY_REQUESTS
- INTERNAL_SERVER_ERROR
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>
impl<'a> HttpResponse<'a>
Sourcepub fn ok(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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!");
Sourcepub fn created(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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!");
Sourcepub fn no_content(headers: Vec<(String, String)>) -> HttpResponseBuilder<'a>
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())]);
Sourcepub fn moved_permanently(
location: impl Into<String>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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())]);
Sourcepub fn not_modified(headers: Vec<(String, String)>) -> HttpResponseBuilder<'a>
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())]);
Sourcepub fn temporary_redirect(
location: impl Into<String>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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())]);
Sourcepub fn bad_request(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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");
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");
Sourcepub fn forbidden(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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");
Sourcepub fn not_found(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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");
Sourcepub fn method_not_allowed(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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");
Sourcepub fn too_many_requests(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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");
Sourcepub fn internal_server_error(
body: impl Into<Cow<'a, [u8]>>,
headers: Vec<(String, String)>,
) -> HttpResponseBuilder<'a>
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");
Sourcepub fn builder() -> HttpResponseBuilder<'a>
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));
Sourcepub fn status_code(&self) -> StatusCode
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);
Sourcepub fn headers(&self) -> &[HeaderField] ⓘ
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())]);
Sourcepub fn headers_mut(&mut self) -> &mut Vec<HeaderField> ⓘ
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())]);
Sourcepub fn add_header(&mut self, header: HeaderField)
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())]);
Sourcepub fn body(&self) -> &[u8] ⓘ
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!");
Sourcepub fn upgrade(&self) -> Option<bool>
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>
impl<'a> CandidType for HttpResponse<'a>
Source§impl<'a> Clone for HttpResponse<'a>
impl<'a> Clone for HttpResponse<'a>
Source§fn clone(&self) -> HttpResponse<'a>
fn clone(&self) -> HttpResponse<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more