#wasmcloud #wasm #actor #webassembly #capability

wasmcloud-interface-httpclient

interface for actors to issue http/https requests (wasmcloud:httpclient)

15 releases (7 breaking)

0.8.1 Nov 23, 2022
0.7.0 Aug 16, 2022
0.6.1 Jul 18, 2022
0.5.0 Mar 1, 2022
0.1.0 Jul 23, 2021

#209 in WebAssembly

Download history 29/week @ 2022-11-27 5/week @ 2022-12-04 21/week @ 2022-12-11 5/week @ 2022-12-18 12/week @ 2022-12-25 9/week @ 2023-01-01 3/week @ 2023-01-08 12/week @ 2023-01-15 28/week @ 2023-01-22 28/week @ 2023-01-29 107/week @ 2023-02-05 50/week @ 2023-02-12 55/week @ 2023-02-19 17/week @ 2023-02-26 3/week @ 2023-03-05 9/week @ 2023-03-12

100 downloads per month

Apache-2.0 and maybe LGPL-3.0-or-later

22KB
433 lines

crates.io  TinyGo Version

wasmCloud HTTP Client Interface

This is the interface definition for the interface with the contract ID wasmcloud:httpclient.

Actors utilizing this interface can make HTTP requests and receive HTTP responses for processing. Since this is just an interface, and not an actual provider, you will need to check the documentation for individual provider implementations for a list of link definition values supported by that provider.

Capability Provider Implementations

The following is a list of implementations of the wasmcloud:httpclient contract. Feel free to submit a PR adding your implementation if you have a community/open source version.

Name Vendor Description
HTTPClient wasmCloud wasmCloud implementation of the HTTP Client Provider

Example Usage (🦀 Rust)

Retrieve a random XKCD comic and format the response as an HTML page

use serde::Deserialize;
use wasmbus_rpc::actor::prelude::*;
use wasmcloud_interface_httpclient::*;
use wasmcloud_interface_httpserver::HttpResponse;
use wasmcloud_interface_numbergen::random_in_range;

const MAX_COMIC_ID: u32 = 2500;

#[derive(Deserialize)]
struct XkcdMetadata {
    title: String,
    img: String,
}

async fn get_comic(ctx: &Context) -> RpcResult<HttpResponse> {
    let comic_num = random_in_range(1, MAX_COMIC_ID).await?;

    // make a request to get the json metadata
    let url = format!("https://xkcd.com/{}/info.0.json", comic_num);
    let client = HttpClientSender::new();
    let resp = client.request(ctx, &HttpRequest::get(&url)).await?;

    if !(200..300).contains(&resp.status_code) {
        return Err(format!("HTTP Request error {}", resp.status_code.to_string(),).into());
    }
    let comic = serde_json::from_slice::<XkcdMetadata>(&resp.body)
        .map_err(|_| "Error deserializing response body")?;
    let html = format!(
        r#"<!DOCTYPE html>
        <html>
        <head>
            <title>Your XKCD random comic</title>
        </head>
        <body>
            <h1>{}</h1>
            <img src="{}"/>
        </body>
        </html>
        "#,
        &comic.title, &comic.img
    );
    let resp = HttpResponse {
        body: html.into_bytes(),
        ..Default::default()
    };
    Ok(resp)
}

Dependencies

~9–20MB
~406K SLoC