Last Updated: 3/9/2026
HTTP Server Inferlet Example
This example demonstrates how to create an HTTP server inferlet using the wasi:http/incoming-handler interface. Unlike regular inferlets that use the inferlet:core/run interface and run to completion, server inferlets handle incoming HTTP requests.
Key Differences from Regular Inferlets
| Aspect | Regular Inferlet | Server Inferlet |
|---|---|---|
| Interface | inferlet:core/run | wasi:http/incoming-handler |
| Entry Point | #[inferlet::main] | #[wstd::http_server] |
| Lifecycle | Runs once to completion | Handles each HTTP request |
| Instance | Single instance per launch | Fresh instance per request |
Endpoints
This example provides the following endpoints:
/- Home page with a greeting and list of available endpoints/wait- Demonstrates async sleep (sleeps for 1 second and reports elapsed time)/echo- Echoes back the request body (useful for testing POST requests)/echo-headers- Echoes back request headers as response headers/info- Returns server and request information as JSON
Building
cd sdk/examples
cargo build -p http-server --target wasm32-wasip2 --releaseRunning
Launch the server inferlet on a specific port:
pie http --path ./target/wasm32-wasip2/release/http_server.wasm --port 8080Testing
Once the server is running, you can test the endpoints:
# Home page
curl http://localhost:8080/
# Async sleep demo
curl http://localhost:8080/wait
# Echo back POST body
curl -X POST -d "Hello, World!" http://localhost:8080/echo
# Echo headers
curl -v http://localhost:8080/echo-headers
# Get request info as JSON
curl http://localhost:8080/info?foo=barCode Structure
The example uses the wstd crate’s #[wstd::http_server] macro, which:
- Sets up the
wasi:http/incoming-handlerexport - Converts the raw WASI HTTP types to ergonomic
RequestandRespondertypes - Handles the async execution of your handler function
Key Components
Request<IncomingBody>- The incoming HTTP request with its bodyResponder- Used to send the HTTP responseFinished- Return type indicating the response has been completedResponse- Builder for constructing HTTP responsesBodyForthcoming- Used for streaming response bodies
Dependencies
This example only requires:
[dependencies]
wstd = "0.5.6"