Skip to content

Latest commit

 

History

History
771 lines (538 loc) · 21.6 KB

File metadata and controls

771 lines (538 loc) · 21.6 KB

ARCHIVE of "Make HTTP request from inside WebAssembly" list

Caution

Approaches listed on this page are deprecated or/and do not longer work. They are collected here for historical purposes.

Each item has hyperlink to up-to-date alternative in the main list.

Contents

Language Browsers and JS runtimes Standalone / server-side / WASI runtimes
AssemblyScript

wasi-experimental-http, wasi-http-ts

C# / .Net

Uno Platform

wasi-experimental-http

C / C++

wasi-experimental-http

Golang / TinyGo

wasi-experimental-http

PHP

PIB aka php-wasm

Python

requests-wasm-polyfill

Rust

wasi-experimental-http

Zig

wasi-experimental-http

Browser WASM runtimes and standalone JS runtimes like Node.js and Deno (and other V8-based), also Bun

C#

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

Uno Platform

Caution

Uno.UI.Wasm. WasmHttpHandler deprecated since .NET 6

Tip

Use System.Net. Http.HttpClient instead.

// Uno Platform's own way
// Deprecated since .NET 6,
// use 'System.Net.Http.HttpClient' and 'HttpHandler' instead
// See https://github.com/unoplatform/uno/issues/9665
using System.Net.Http;

using (var handler = new Uno.UI.Wasm.WasmHttpHandler())
using (var client = new HttpClient(handler))
{
var request = new HttpRequestMessage(HttpMethod.Get,
  new Uri("https://httpbin.org/anything"));
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
}

Test

Tutorial

Playground

Browser

JS Interop invokes the TS wrapper for fetch

PHP

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

PIB: PHP in Browser aka php-wasm

Caution

This code does not work in php-wasm v0.0.8.

Tip

Use up-to-date method instead.

$js=<<<JSCODE
var xhr=new XMLHttpRequest();
xhr.open('GET', 'https://httpbin.org/anything', 0);
xhr.send();

xhr.responseText;
JSCODE;

var_dump(vrzno_eval($js));

Example

Doc

Browser, Node.js, and maybe Deno

Manual JS XMLHttpRequest interop using vrzno PHP extension.

Python

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

requests-wasm-polyfill

For pyjs

Caution

Repo was archived and package was dropped from recipes on Feb 6, 2024.

Tip

Use the official requests instead.

import requests

r = requests.get('https://httpbin.org/anything')
result = r.text
print(result)

Example

Browser

Direct XMLHttpRequest interop in sync mode.

Standalone and server-side runtimes (mostly WASI and WASI-Socket-enabled), incl containers, FaaS, Edge Computing, etc

AssemblyScript

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

wasi-experimental-http

Caution

Repo was archived on Feb 22, 2024.

Tip

Use wasi-http instead.

import { 
  Method, RequestBuilder, Response 
} from "@deislabs/wasi-experimental-http";
import { Console } from "as-wasi/assembly";

let resp = new RequestBuilder("https://httpbin.org/anything")
    .header("User-Agent", "wasm32-wasi-http")
    .method(Method.GET)
    .send();

let text = String.UTF8.decode(resp.bodyReadAll().buffer);
Console.log(text);

Readme

Dev Container created by brendandburns

Wasmtime with integrated wasi-experimental-http crate, e.g. brendandburns's fork or old Spin

Importing npm package which calls imported host function implemented in wasi-experimental-http-wasmtime Wasmtime's WASI module.

brendandburns/wasi-http-ts

Caution

Repo was archived on Feb 22, 2024.

Tip

Use wasi-http instead.

// Identical to wasi-experimental-http currently
// Needs `npm i https://github.com/brendandburns/wasi-http-ts`
import { 
  Method, RequestBuilder 
} from "@brendandburns/wasi-http-ts";
import { Console } from "as-wasi/assembly";

let resp = new RequestBuilder("https://httpbin.org/anything")
    .header("User-Agent", "wasm32-wasi-http")
    .method(Method.GET)
    .send();

let text = String.UTF8.decode(resp.bodyReadAll().buffer);
Console.log(text);

<--

Readme from dev-wasm-ts

Possible with Dev Container created by brendandburns

Wasmtime with integrated wasi-experimental-http crate, e.g. brendandburns's fork

Using client lib which calls imported host function implemented in wasi-experimental-http-wasmtime Wasmtime's WASI module.

C# / .Net

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

wasi-experimental-http

Caution

Repo was archived on Feb 22, 2024.

Tip

Use wasi-http instead.

using Wasi.Http;

var client = new HttpClient(new WasiHttpHandler());
var result = await client.GetAsync("https://httpbin.org/anything", 
 System.Threading.CancellationToken.None);
var content = await result.Content.ReadAsStringAsync();
Console.WriteLine(content);

Example

Readme

Dev Container created by brendandburns

Wasmtime with integrated wasi-experimental-http crate, e.g. brendandburns's fork

Calling imported via C level bindings the host function implemented in wasi-experimental-http-wasmtime Wasmtime's WASI module.

C / C++

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

wasi-experimental-http

Caution

Repo was archived on Feb 22, 2024.

Tip

Use wasi-http instead.

#include "req.h"
#include <string.h>
#include <stdio.h>

const char* url =  "https://httpbin.org/anything";
const char* headers = "User-agent: wasm32-wasi-http";
size_t length = 1024 * 1024;
char* buffer = (char*) malloc(length);
uint16_t code;
ResponseHandle handle;

HttpError err = req(url, strlen(url), "GET", 3, headers, 
  strlen(headers), "", 0, &code, &handle);

size_t written;
err = bodyRead(handle, buffer, length, &written);
close(handle);

printf("%s\n", buffer);

free(buffer);

Example

Readme

Dev Container created by brendandburns

Wasmtime with integrated wasi-experimental-http crate, e.g. brendandburns's fork

Calling imported host function implemented in wasi-experimental-http-wasmtime Wasmtime's WASI module.

Golang (Tinygo)

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

wasi-experimental-http

Caution

Repo was archived on Feb 22, 2024.

Tip

Use wasi-http instead.

response, err := Request(
  "https://httpbin.org/anything", "GET",
  "User-agent: wasm32-wasi-http", "")
defer response.Close()

body, err := response.Body()
if err == nil {
  println(string(body))
}

Example

Readme

Dev Container created by brendandburns

Wasmtime with integrated wasi-experimental-http crate, e.g. brendandburns's fork

Calling imported host function implemented in wasi-experimental-http-wasmtime Wasmtime's WASI module.

Rust

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

wasi-experimental-http

Caution

Repo was archived on Feb 22, 2024.

Tip

Use wasi-http instead.

use http;
use wasi_experimental_http;

let req = http::request::Builder::new()
  .uri("https://httpbin.org/anything").body(None).unwrap();
let resp = wasi_experimental_http::request(req);
let data = std::str::from_utf8(&resp.body_read_all().unwrap())
  .unwrap().to_string();
println!("{}", data);

Test

Doc

Wasmtime with integrated wasi-experimental-http crate, e.g. brendandburns's fork

Calling imported Wasmtime's host function. The actual request is here.

Zig

Product / ImplementationTLDR: UsageTLDR: Example code Doc Online demo WASM RuntimeInternals: method to do real request

wasi-experimental-http

Caution

Repo was archived on Feb 22, 2024.

Tip

Use wasi-http instead.

var gp_all = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gp_all.allocator();
var status: u16 = 0;
var handle: u32 = 0;

_ = request("https://httpbin.org/anything", "GET", "", "", &status, &handle);
defer _ = close(@bitCast(i32, handle));

var headers_buffer = try allocator.alloc(u8, 1024);
var header_len: u32 = 0;
_ = header(handle, "content-length", headers_buffer, &header_len);
const length = try std.fmt.parseInt(u32, headers_buffer[0..header_len], 10);
var buffer = try allocator.alloc(u8, length + 1);
defer allocator.free(buffer);

var written: u32 = 0;
_ = body_read(handle, &buffer[0], @bitCast(i32, buffer.len), &written);
try std.io.getStdOut().writer().print("{s}\n", .{buffer[0..written]});

Example

Doc

Dev Container by brendandburns

Wasmtime with integrated wasi-experimental-http crate, e.g. brendandburns's fork

Calling imported Wasmtime's host function. The actual request is here.