9 个重大版本发布

0.10.0 2022年6月15日
0.9.0 2022年2月19日
0.8.0 2022年1月21日
0.7.0 2021年11月15日
0.2.0 2021年3月24日

#400 in WebAssembly


用于 4 crates

MIT 许可证

22KB
498

wasi-experimental-http

Crates.io

WebAssembly 实验性 HTTP 库

使用此包

首先,将此包添加到您的项目中。然后,可以使用它来创建并向服务器发送 HTTP 请求

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);
}

使用 wasm32-wasi 目标构建模块,然后在启用实验性 HTTP 功能的 Wasmtime 运行时中执行(配置此功能的包可以在此仓库中找到)

{
    "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"

依赖项

~1.2–2MB
~37K SLoC