Network requests
Components can make network requests to external APIs using the wavs-wasi-utils
crate.
To learn how to use variables like API keys in a component, visit the Variables page.
Dependencies
The following dependencies are required for making HTTP requests from a component:
[dependencies]wavs-wasi-utils = "0.4.0" # HTTP utilitieswstd = "0.5.3" # Runtime utilities (includes block_on)serde = { version = "1.0.219", features = ["derive"] } # Serializationserde_json = "1.0.140" # JSON handling
Since WASI components run in a synchronous environment but network requests are asynchronous, you can use block_on
from the wstd
crate to bridge this gap. The block_on
function allows you to run async code within a synchronous context, which is essential for making HTTP requests in WAVS components.
Making HTTP requests
The wavs-wasi-utils
crate provides several functions for making HTTP requests. See the HTTP module documentation for more details.
// Request functionshttp_request_get(url) // Creates a GET requesthttp_request_post_json(url, data) // Creates a POST request with JSON datahttp_request_post_form(url, data) // Creates a POST request with form data// Response functionsfetch_json(request) // Fetches and parses JSON responsefetch_string(request) // Fetches response as stringfetch_bytes(request) // Fetches raw response bytes
Example: GET request with headers
Here's an example showing how to make a GET request with custom headers:
use wstd::runtime::block_on;use wstd::http::HeaderValue;use wavs_wasi_utils::http::{fetch_json, http_request_get};use serde::{Deserialize, Serialize};// Define response type with serde derive for automatic JSON parsing#[derive(Debug, Serialize, Deserialize)]struct ApiResponse {// ... your response fields}async fn make_request() -> Result<ApiResponse, String> {let url = "https://api.example.com/endpoint";let mut req = http_request_get(&url).map_err(|e| e.to_string())?;// Set required headers for API requestsreq.headers_mut().insert("Accept",HeaderValue::from_static("application/json"));req.headers_mut().insert("Content-Type",HeaderValue::from_static("application/json"));req.headers_mut().insert("User-Agent",HeaderValue::from_static("Mozilla/5.0"));// Use fetch_json to automatically parse the responselet json: ApiResponse = fetch_json(req).await.map_err(|e| e.to_string())?;Ok(json)}// Use block_on to handle async code in sync contextfn process_data() -> Result<ApiResponse, String> {block_on(async move {make_request().await})?}
Example: POST request with JSON data
For making POST requests with JSON data, you can use the http_request_post_json
helper function:
use wstd::runtime::block_on;use wavs_wasi_utils::http::{fetch_json, http_request_post_json};use serde::{Deserialize, Serialize};// Define request and response types with serde derive#[derive(Debug, Serialize, Deserialize)]struct PostData {key1: String,key2: i32,}#[derive(Debug, Serialize, Deserialize)]struct PostResponse {// ... response fields}async fn make_post_request() -> Result<PostResponse, String> {let url = "https://api.example.com/endpoint";let post_data = PostData {key1: "value1".to_string(),key2: 42,};// http_request_post_json automatically sets JSON headerslet response: PostResponse = fetch_json(http_request_post_json(&url, &post_data)?).await.map_err(|e| e.to_string())?;Ok(response)}fn process_data() -> Result<PostResponse, String> {block_on(async move {make_post_request().await})?}
For more details, visit the wavs-wasi-utils documentation.