4 releases

0.2.1 Aug 22, 2024
0.2.0 Aug 22, 2024
0.1.1 Aug 21, 2024
0.1.0 Aug 21, 2024

#368 in WebAssembly

21 downloads per month

MIT license

16KB
291 lines

teahttpd

an unsophisticated (fetch wrapper) http client for wasm

ok pls propose something because this ductape plane is running on a motive of "should works, would works and it works!"

how to use

    let _res: web_sys::Request = teahttp::TeaRequest::get("/api/something")
        .header("Accept", "application/json")?
        .clone()
        .slice_body(&encoded)
        .invoke()
        .await?;

    let _res: web_sys::Request = teahttp::TeaRequest::post("/api/upload")
        .header("Content-Type", "application/octet-stream")?
        .header("Content-Length", &encoded.len().to_string())?
        .clone()
        .slice_body(&encoded)
        .invoke()
        .await?;

todo if needed

  • better error handling
  • serde
  • umm diy and wrap this?

lib.rs:

teahttp

Very simple but work well for WASM environment. As many http client tend to screw up WebWorker global, this ehttp inspired crate is to help. Although it seems to be a good alt, many things considerably manual.

Few example to figure things out

    TeaRequest::get("/api/something")
       .invoke()
       .await? // web_sys::Response

    let some_body = b"lorem ipsum dolor si amet";
    TeaRequest::post("/api/upload")
        .header("Content-Length", some_body)?
        .slice_body(some_body.as_slice() /* &[u8] */)
        .invoke()
        .await?

    TeaRequest::post("/api/submit")
        .header("Content-Length", &12.to_string())?
        .str_body("Hello World!" /* &str */)
        .invoke()
        .await?

Have fun

Dependencies

~7.5–10MB
~183K SLoC