I am developing a NODE JS package using Rust and wasm-pack
to compile, and I need to make HTTP requests in my code. I tried to use reqwest
library, so everything works fine in the test but I get an error in packing.
#![allow(non_snake_case)]
use reqwest;
use wasm_bindgen::prelude::*;
// function
#[wasm_bindgen]
pub fn makeGetRequest(url: &str) -> String {
let mut resp = reqwest::get(url).unwrap();
resp.text().unwrap()
}
// test
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_makeGetRequest() {
let url = "https://stackoverflow.com/";
let body = makeGetRequest(url);
assert!(body.len() > 0);
}
}
Configuration Cargo.toml
:
...
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
reqwest = "0.9"
I pack the project with the command:
wasm-pack build --release --target nodejs
And I get the error:
...
error: could not compile `net2`
I found that it looks like net2
is not supported in wasm-pack
, so perhaps I cannot use reqwest
in my case: wasm-pack build report error: could not compile `net2`
Is there a way to make synchronous HTTP requests that can be packed with wasm-pack
successfully?