How to send an offchain http post request

Viewed 79

How do i send an offchain http post request and fetch a hash?

fn fetch(verify: T::Hash, number: Vec<u8>) -> Result<Vec<u8>, &'static str> {
        let http_remote: &str = "http://localhost:3000/endpoint";
        let pending = rt_offchain::http::Request::post(http_remote, verify)
            .send()
            .map_err(|_| "Error in sending http post request")?;

        let response = pending.wait()
            .map_err(|_| "Error in waiting http response back")?;
        if response.code != 200 {
            debug::warn!("Unexpected status code: {}", response.code);
            return Err("Non-200 status code returned from http request");
        }
        Ok(response.body().collect::<Vec<u8>>())
    }
0 Answers
Related