Struct HttpRequest

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

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

§Examples

use ic_http_certification::{HttpRequest, Method};

let request = HttpRequest::builder()
    .with_method(Method::GET)
    .with_url("/")
    .with_headers(vec![("X-Custom-Foo".into(), "Bar".into())])
    .with_body(&[1, 2, 3])
    .with_certificate_version(2)
    .build();

assert_eq!(request.method(), Method::GET);
assert_eq!(request.url(), "/");
assert_eq!(request.headers(), &[("X-Custom-Foo".into(), "Bar".into())]);
assert_eq!(request.body(), &[1, 2, 3]);
assert_eq!(request.certificate_version(), Some(2));

§Helpers

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

use ic_http_certification::HttpRequest;

let request = HttpRequest::get("/").build();

assert_eq!(request.method(), "GET");
assert_eq!(request.url(), "/");

Implementations§

Source§

impl<'a> HttpRequest<'a>

Source

pub fn get(url: impl Into<String>) -> HttpRequestBuilder<'a>

Creates a new HttpRequestBuilder initialized with a GET method and the given URL.

This method returns an instance of HttpRequestBuilder which can be used to create an HttpRequest.

§Examples
use ic_http_certification::{HttpRequest, Method};

let request = HttpRequest::get("/").build();

assert_eq!(request.method(), Method::GET);
Source

pub fn post(url: impl Into<String>) -> HttpRequestBuilder<'a>

Creates a new HttpRequestBuilder initialized with a POST method and the given URL.

This method returns an instance of HttpRequestBuilder which can be used to create an HttpRequest.

§Examples
use ic_http_certification::{HttpRequest, Method};

let request = HttpRequest::post("/").build();

assert_eq!(request.method(), Method::POST);
Source

pub fn put(url: impl Into<String>) -> HttpRequestBuilder<'a>

Creates a new HttpRequestBuilder initialized with a PUT method and the given URL.

This method returns an instance of HttpRequestBuilder which can be used to create an HttpRequest.

§Examples
use ic_http_certification::{HttpRequest, Method};

let request = HttpRequest::put("/").build();

assert_eq!(request.method(), Method::PUT);
Source

pub fn patch(url: impl Into<String>) -> HttpRequestBuilder<'a>

Creates a new HttpRequestBuilder initialized with a PATCH method and the given URL.

This method returns an instance of HttpRequestBuilder which can be used to create an HttpRequest.

§Examples
use ic_http_certification::{HttpRequest, Method};

let request = HttpRequest::patch("/").build();

assert_eq!(request.method(), Method::PATCH);
Source

pub fn delete(url: impl Into<String>) -> HttpRequestBuilder<'a>

Creates a new HttpRequestBuilder initialized with a DELETE method and the given URL.

This method returns an instance of HttpRequestBuilder which can be used to create an HttpRequest.

§Examples
use ic_http_certification::{HttpRequest, Method};

let request = HttpRequest::delete("/").build();

assert_eq!(request.method(), Method::DELETE);
Source

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

Creates and returns an instance of HttpRequestBuilder, a builder-style object which can be used to create an HttpRequest.

§Examples
use ic_http_certification::{HttpRequest, Method};

let request = HttpRequest::builder()
    .with_method(Method::GET)
    .with_url("/")
    .with_headers(vec![("X-Custom-Foo".into(), "Bar".into())])
    .with_body(&[1, 2, 3])
    .with_certificate_version(2)
    .build();

assert_eq!(request.method(), Method::GET);
assert_eq!(request.url(), "/");
assert_eq!(request.headers(), &[("X-Custom-Foo".into(), "Bar".into())]);
assert_eq!(request.body(), &[1, 2, 3]);
assert_eq!(request.certificate_version(), Some(2));
Source

pub fn method(&self) -> &Method

Returns the HTTP method of the request.

§Examples
use ic_http_certification::HttpRequest;

let request = HttpRequest::get("/").build();

assert_eq!(request.method(), "GET");
Source

pub fn url(&self) -> &str

Returns the URL of the request.

§Examples
use ic_http_certification::HttpRequest;

let request = HttpRequest::get("/").build();

assert_eq!(request.url(), "/");
Source

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

Returns the headers of the request.

§Examples
use ic_http_certification::HttpRequest;

let request = HttpRequest::get("/")
    .with_headers(vec![("Accept".into(), "text/plain".into())])
    .build();

assert_eq!(request.headers(), &[("Accept".into(), "text/plain".into())]);
Source

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

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

§Examples
use ic_http_certification::HttpRequest;

let mut request = HttpRequest::get("/")
    .with_headers(vec![("Content-Type".into(), "text/plain".into())])
    .build();

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

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

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

Returns the body of the request.

§Examples
use ic_http_certification::HttpRequest;

let request = HttpRequest::get("/")
    .with_body(&[1, 2, 3])
    .build();

assert_eq!(request.body(), &[1, 2, 3]);
Source

pub fn certificate_version(&self) -> Option<u16>

Returns the max response verification version to use in the response’s certificate.

§Examples
use ic_http_certification::HttpRequest;

let request = HttpRequest::get("/")
    .with_certificate_version(2)
    .build();

assert_eq!(request.certificate_version(), Some(2));
Source

pub fn get_path(&self) -> HttpCertificationResult<String>

Returns the path of the request URL, without domain, query parameters or fragments.

§Examples
use ic_http_certification::HttpRequest;

let request = HttpRequest::get("https://canister.com/sample-asset.txt").build();

assert_eq!(request.get_path().unwrap(), "/sample-asset.txt");
Source

pub fn get_query(&self) -> HttpCertificationResult<Option<String>>

Returns the query parameters of the request URL, if any, as a string.

§Examples
use ic_http_certification::HttpRequest;

let request = HttpRequest::get("https://canister.com/sample-asset.txt?foo=bar").build();

assert_eq!(request.get_query().unwrap(), Some("foo=bar".to_string()));

Trait Implementations§

Source§

impl<'a> CandidType for HttpRequest<'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 HttpRequest<'a>

Source§

fn clone(&self) -> HttpRequest<'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<'a> Debug for HttpRequest<'a>

Source§

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

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

impl<'de, 'a> Deserialize<'de> for HttpRequest<'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<HttpRequest<'a>> for HttpUpdateRequest<'a>

Source§

fn from(req: HttpRequest<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> PartialEq for HttpRequest<'a>

Source§

fn eq(&self, other: &HttpRequest<'a>) -> 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.
Source§

impl<'a> Eq for HttpRequest<'a>

Source§

impl<'a> StructuralPartialEq for HttpRequest<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for HttpRequest<'a>

§

impl<'a> RefUnwindSafe for HttpRequest<'a>

§

impl<'a> Send for HttpRequest<'a>

§

impl<'a> Sync for HttpRequest<'a>

§

impl<'a> Unpin for HttpRequest<'a>

§

impl<'a> UnwindSafe for HttpRequest<'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>,