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>
impl<'a> HttpRequest<'a>
Sourcepub fn get(url: impl Into<String>) -> HttpRequestBuilder<'a>
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);
Sourcepub fn post(url: impl Into<String>) -> HttpRequestBuilder<'a>
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);
Sourcepub fn put(url: impl Into<String>) -> HttpRequestBuilder<'a>
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);
Sourcepub fn patch(url: impl Into<String>) -> HttpRequestBuilder<'a>
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);
Sourcepub fn delete(url: impl Into<String>) -> HttpRequestBuilder<'a>
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);
Sourcepub fn builder() -> HttpRequestBuilder<'a>
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));
Sourcepub fn method(&self) -> &Method
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");
Sourcepub fn url(&self) -> &str
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(), "/");
Sourcepub fn headers(&self) -> &[HeaderField] ⓘ
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())]);
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 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())]);
Sourcepub fn body(&self) -> &[u8] ⓘ
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]);
Sourcepub fn certificate_version(&self) -> Option<u16>
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));
Sourcepub fn get_path(&self) -> HttpCertificationResult<String>
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");
Sourcepub fn get_query(&self) -> HttpCertificationResult<Option<String>>
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>
impl<'a> CandidType for HttpRequest<'a>
Source§impl<'a> Clone for HttpRequest<'a>
impl<'a> Clone for HttpRequest<'a>
Source§fn clone(&self) -> HttpRequest<'a>
fn clone(&self) -> HttpRequest<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more