#wasmcloud #actor #capability #api-bindings #http-interface #capability-provider

wasmcloud-interface-httpclient

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

18 releases (10 breaking)

0.11.0 Sep 19, 2023
0.10.0 Jul 20, 2023
0.9.0 Apr 12, 2023
0.8.1 Nov 23, 2022
0.1.0 Jul 23, 2021

#83 in WebAssembly

30 downloads per month
Used in dtbh_interface

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

23KB
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)
}

🐭 Golang

Fetch IP address

func GetIpAddress(ctx *actor.Context) ([]byte, error) {
	client := httpclient.NewProviderHttpClient()

	resp, err := client.Request(ctx, httpclient.HttpRequest{
		Method: "GET",
		Url:    "https://ifconfig.io/ip",
		// Body can not be blank due to a bug
		Body: []byte("a"),
	})
	if err != nil {
		return nil, err
	}

	return resp.Body, nil
}

Dependencies

~13–31MB
~505K SLoC