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 ```rust use serde::Deserialize; use wasmbusrpc::actor::prelude::*; use wasmcloudinterfacehttpclient::*; use wasmcloudinterfacehttpserver::HttpResponse; use wasmcloudinterfacenumbergen::randomin_range;

const MAXCOMICID: u32 = 2500;

[derive(Deserialize)]

struct XkcdMetadata { title: String, img: String, }

async fn getcomic(ctx: &Context) -> RpcResult { let comicnum = randominrange(1, MAXCOMICID).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 ```go 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

} ```