referrerpolicy=no-referrer-when-downgrade

Module sp_runtime::offchain::http

source ·
Expand description

A high-level helpers for making HTTP requests from Offchain Workers.

sp-io crate exposes a low level methods to make and control HTTP requests available only for Offchain Workers. Those might be hard to use and usually that level of control is not really necessary. This module aims to provide high-level wrappers for those APIs to simplify making HTTP requests.

Example:

use sp_runtime::offchain::http::Request;

// initiate a GET request to localhost:1234
let request: Request = Request::get("http://localhost:1234");
let pending = request
	.add_header("X-Auth", "hunter2")
	.send()
	.unwrap();

// wait for the response indefinitely
let mut response = pending.wait().unwrap();

// then check the headers
let mut headers = response.headers().into_iter();
assert_eq!(headers.current(), None);

// and collect the body
let body = response.body();
assert_eq!(body.clone().collect::<Vec<_>>(), b"1234".to_vec());
assert_eq!(body.error(), &None);

Structs§

Enums§

Type Aliases§

  • A result of waiting for a pending request.