Use .Net's System.Net.Http.HttpClient to send HTTP(s) requests from inside WASM in WASI Preview 2 environment
Tested with .Net SDK version 10.0.102, official WASIp2 example tagged v10.0.2, Wasmtime v41.0.1.
-
Open this repo in devcontainer, e.g. using Github Codespaces. Type or copy/paste following commands to devcontainer's terminal.
-
Install .Net workload to handle WASI:
dotnet workload install wasi-experimentalcdinto the folder of this example:
cd wasip2-http- Create new .Net project using
wasiconsoletemplate:
dotnet new wasiconsole -n Wasip2-Httpcdinto the folder of the new .Net project to use it:
cd Wasip2-Http- Replace generated HelloWorld-like
Program.cswith downloaded official http-p2 example:
rm Program.cs
wget https://github.com/dotnet/runtime/raw/refs/tags/v10.0.2/src/mono/sample/wasi/http-p2/Program.cs- Compile the example:
dotnet build -c Release- Install Wasmtime:
curl https://wasmtime.dev/install.sh -sSf | bashcdinto the folder of just-compiled binaries bundle:
cd bin/*/net*/wasi-wasm/AppBundle- Run Wasmtime with HTTP plugin enabled against just-compiled HTTP Client example:
~/.wasmtime/bin/wasmtime run --dir . -S http dotnet.wasm Wasip2-Http- See the results in terminal.
Perform your own experiments if desired.