#http #experimental #wasi #send-http #http-request #byte

lapce-wasi-experimental-http

Experimental HTTP library for WebAssembly

1 unstable release

0.10.0 Jan 10, 2023

#1013 in WebAssembly

Download history 38/week @ 2024-03-11 22/week @ 2024-03-18 12/week @ 2024-03-25 54/week @ 2024-04-01 19/week @ 2024-04-08 14/week @ 2024-04-15 15/week @ 2024-04-22 13/week @ 2024-04-29 14/week @ 2024-05-06 15/week @ 2024-05-13 16/week @ 2024-05-20 23/week @ 2024-05-27 23/week @ 2024-06-03 17/week @ 2024-06-10 17/week @ 2024-06-17 18/week @ 2024-06-24

78 downloads per month
Used in lapce-plugin

MIT license

22KB
498 lines

wasi-experimental-http

Crates.io

Experimental HTTP library for WebAssembly

Using the crate

First, add this crate to your project. Then, it can be used to create and send an HTTP request to a server:

use bytes::Bytes;
use http;
use wasi_experimental_http;

#[no_mangle]
pub extern "C" fn _start() {
    let url = "https://postman-echo.com/post".to_string();
    let req = http::request::Builder::new()
        .method(http::Method::POST)
        .uri(&url)
        .header("Content-Type", "text/plain")
        .header("abc", "def");
    let b = Bytes::from("Testing with a request body. Does this actually work?");
    let req = req.body(Some(b)).unwrap();

    let res = wasi_experimental_http::request(req).expect("cannot make request");
    let str = std::str::from_utf8(&res.body_read_all()).unwrap().to_string();
    println!("{:#?}", res.header_get("Content-Type"));
    println!("{}", str);
    println!("{:#?}", res.status_code);
}

Build the module using the wasm32-wasi target, then execute in a Wasmtime runtime that has the experimental HTTP functionality enabled (the crate to configure it can be found in this repo):

{
    "content-length": "374",
    "connection": "keep-alive",
    "set-cookie": "sails.Path=/; HttpOnly",
    "vary": "Accept-Encoding",
    "content-type": "application/json; charset=utf-8",
    "date": "Fri, 26 Feb 2021 18:31:03 GMT",
    "etag": "W/\"176-Ky4OTmr3Xbcl3yNah8w2XIQapGU\"",
}
{"args":{},"data":"Testing with a request body. Does this actually work?","files":{},"form":{},"headers":{"x-forwarded-proto":"https","x-forwarded-port":"443","host":"postman-echo.com","x-amzn-trace-id":"Root=1-60393e67-02d1c8033bcf4f1e74a4523e","content-length":"53","content-type":"text/plain","abc":"def","accept":"*/*"},"json":null,"url":"https://postman-echo.com/post"}
"200 OK"

Dependencies

~1.3–2MB
~38K SLoC