Skip to content

Create new section on how to use HTTP in components#336

Open
hakilebara wants to merge 1 commit intobytecodealliance:mainfrom
hakilebara:http-example
Open

Create new section on how to use HTTP in components#336
hakilebara wants to merge 1 commit intobytecodealliance:mainfrom
hakilebara:http-example

Conversation

@hakilebara
Copy link
Copy Markdown
Contributor

Create a new section in Creating Components that focuses on using HTTP in components.
It includes a Rust guide on how to create a basic Hello, world! HTTP component using wstd.

 ├── Creating Components
 │   └── Building a simple component
 │   └── Importing and reusing components
 │   └── Creating Runnable components
 │   └── Using WIT resources
+│   ├── Using HTTP in components
+|   |   └── Rust

#182

Copy link
Copy Markdown
Collaborator

@kate-goldenring kate-goldenring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Ran through it all in just a minute. Thank you for adding this! One small nit suggestion

cd wasm-http-hello-world
```

Add [`wstd`][wstd], a Rust async standard library for Wasm Components.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate here what wstd does for us? Maybe explain that it prevents the need to directly pull the WASI HTTP WITs and directly use the bindings generated by wit-bindgen.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt?

image

Includes a Rust guide on creating an HTTP Wasm component with wstd.
Copy link
Copy Markdown
Collaborator

@kate-goldenring kate-goldenring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small reword and then i think we are good to go



> [!NOTE]
> You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid.
> You can build an HTTP component in Rust without `wstd`. You would have to fetch the [WASI HTTP WIT files](https://github.com/WebAssembly/WASI/tree/main/proposals/http) and their dependencies with `wkg` and then generate the Rust bindings with `wit-bindgen`. Both approaches are valid.

@kate-goldenring
Copy link
Copy Markdown
Collaborator

kate-goldenring commented Apr 16, 2026

closes #182

@kate-goldenring kate-goldenring linked an issue Apr 16, 2026 that may be closed by this pull request
Copy link
Copy Markdown
Collaborator

@vados-cosmonic vados-cosmonic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

I left a few nits -- please feel free to rewrite/reword things as you'd like. I've also tried to add more links to things that readers may not be immediately familiar with (for example if they just landed directly on this page).

```console
cargo add wstd
```
`wstd` provides bindings for `wasi:http` as an idiomatic Rust async API. That's why, in this guide, you won't add WIT files to your project nor depend on `wit-bindgen` directly.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`wstd` provides bindings for `wasi:http` as an idiomatic Rust async API. That's why, in this guide, you won't add WIT files to your project nor depend on `wit-bindgen` directly.
`wstd` provides idiomatic Rust bindings for WASI standard interfaces (`wasi:http`) to increase ease-of-use for Rust WebAssembly components. Since we are using `wstd`, we will not need to add WIT files or depend on [`wit-bindgen`](https://crates.io/crates/wit-bindgen) directly.

Slight re-wording of this paragraph

```


Add [`wstd`][wstd], a Rust async standard library for Wasm components.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Add [`wstd`][wstd], a Rust async standard library for Wasm components.
Add [`wstd`][wstd], a Rust async standard library for Wasm components as a dependency with `cargo add`:



> [!NOTE]
> You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> You can build an HTTP component in Rust without `wstd`. You would have to define the interfaces of the component in the WIT language, fetch their dependencies with `wkg` and generate the Rust bindings with `wit-bindgen`. Both approaches are valid.
> It is possible to build an HTTP component in Rust without `wstd`. Building a HTTP component without `wstd` would require defining the `wasi:http` imports/exports of the component in WIT, fetching WIT dependencies with `wkg` and generating the Rust bindings with `wit-bindgen`.
>
> Both approaches are valid, but `wstd` offers superior developer experience, so we opt to use it here.

// transforms the user's `fn main` into the appropriate `handle` function.
#[wstd::http_server]
async fn main(req: Request<Body>) -> Result<Response<Body>> {
match req.uri().path_and_query().unwrap().as_str() {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
match req.uri().path_and_query().unwrap().as_str() {
match req.uri().path() {

We can use just path() here

```


The resulting `.wasm` file will be compiled in `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The resulting `.wasm` file will be compiled in `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`.
The `.wasm` binary for the component can be found at `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`.

The resulting `.wasm` file will be compiled in `target/wasm32-wasip2/release/wasm-http-hello-world.wasm`.


Run the following Wasmtime command, it will spin-up an HTTP server on [`http://0.0.0.0:8080`](http://0.0.0.0:8080/) that redirect requests to instances of our component.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Run the following Wasmtime command, it will spin-up an HTTP server on [`http://0.0.0.0:8080`](http://0.0.0.0:8080/) that redirect requests to instances of our component.
To run the component, we can use [`wasmtime`](https://github.com/bytecodealliance/wasmtime/), a reference implementation host that supports the Component Model.
In particular, we can use `wasmtime serve` subcommand, which will spin-up an HTTP server at `http://localhost:8080` which will use our component to fulfill web requests. `wasmtime` creates a *fresh* instance of the component every time a request is served.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add WASI HTTP example

3 participants