Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
How do I make an HTTP request within a wasm_bindgen function in Rust?

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It won't work as easily as you'd expect: WASM bytecode is executed in a protected environment without any access to OS features like disk, network, conventional random generators and any other type of I/O. Consequently, the moment you compile any Rust code using such features into Wasm, it won't work.

Unfortunately your code (e.g. file access) usually even compiles silently and then fails in mysterious ways during runtime. This is not what you got used to using Rust and a major drawback of the current Wasm Rust stack.

To access OS features, you'll need WASI (Wasm System Interface) as an extension. To enable Wasi in NodeJs, you can use something like WasmerJs, see e.g. this article providing a short summary to do so.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda