Using HTTP Client
Activities can make HTTP requests using the WASI 0.2 HTTP client. This allows them to interact with external services and APIs. Importantly, this HTTP client is controlled, with built-in tracing to enhance security and predictability. Disk I/O is currently not permitted.
Let's generate a simple HTTP Client from a template:
cargo generate obeli-sk/obelisk-templates activity-rs/http-simple-sync --name httpactivity
cd httpactivity
This will generate a directory with the following structure:
.
├── .cargo
│ └── config.toml
├── Cargo.toml
├── obelisk.toml
├── README.md
├── rust-toolchain.toml
├── src
│ └── lib.rs
└── wit
├── deps
│ └── template-http_activity
│ └── http-get.wit
└── impl.wit
The WIT interface is stored in http-get.wit
:
package template-http:activity;
interface http-get {
get: func(url: string) -> result<string, string>;
}
Instead of using the WASI HTTP 0.2 bindings directly we use the waki HTTP client.
impl Guest for Component {
fn get(url: String) -> Result<String, String> {
let resp = waki::Client::new()
.get(&url)
.connect_timeout(Duration::from_secs(1))
.send()
.map_err(|err| format!("{err:?}"))?;
let body = resp.body().map_err(|err| format!("{err:?}"))?;
Ok(String::from_utf8_lossy(&body).into_owned())
}
}
#[cfg(test)]
mod tests {
#[test]
fn integration_test() {
let url = std::env::var("TEST_URL").expect("TEST_URL must be set");
let body = Component::get(url).unwrap();
println!("body: {body}");
}
}
We can build, run and publish the WASM Component into an OCI registry. See the sync http template for details on how to run the integration test.
If you need to use GraphQL, check out the cynic crate and the graphql template.
For more advanced use cases that require async
support please check out the wstd crate and the async http template.