
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?;
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: []byte("a"),
})
if err != nil {
return nil, err
}
return resp.Body, nil
}