This crate is a Foreign Function Interface (FFI) that allows Rust
code to call the JavaScript fetch
API when deployed on WASM.
The main differences between this crate and using the web_sys
-provided
version of the fetch
API directly are:
- The
web_sys
-provided version only works in the browser. It will compile when targeting NodeJS, but will fail at runtime. - Ergonomics. The
web_sys
version is not very rusty, while this crate aims to be.
NOTE Keep in mind that:
- This crate is only usable on WASM. This means that any crate that uses
fetch-rs
will stop compiling to native code. - Because the
fetch
API is inherentlyasync
, so is this API. This property is infectious, meaning that any code using this crate would to well to convert to async as well. - There are limitations that are imposed by virtue of depending on the node-fetch NPM package. See the NPM page for more details on that.
If you aim to test code using e.g. NodeJS or the browser, install
wasm-pack
by executing cargo install wasm-pack
.
Add this to your Cargo.toml:
[dependencies]
fetch-rs = "0.1"
GET
-request:
use fetch_rs::Request;
#[wasm_bindgen] // <-- This makes the fn callable from JavaScript for easy testing
pub async fn perform_get_request(url: String) -> JsValue {
let result: JsValue = Request::get(&url)
.fetch_text() // fetch the result as text
.await;
info!("[test] result: {:?}", result); // Write test output
result // Since the fn is async, return a Promise / Future
}
POST
-request:
use fetch_rs::Request;
#[wasm_bindgen] // <-- This makes the fn callable from JavaScript for easy testing
pub async fn perform_post_request(url: String) -> JsValue {
let result: JsValue = Request::post(&url)
.fetch_text() // fetch the result as text
.await;
info!("[test] result: {:?}", result); // Write test output
result // Since the fn is async, return a Promise / Future
}
For more examples, check out the examples module in the src directory.