Repository: seanmonstar/reqwest (language: rust)

Issue:
Cannot Use Request Body in Web Target
When using any kind of body in a request on the wasm-bindgen target, the body is converted to an array and sent as a comma-separated list of byte values instead of using the value as-is.

![image](https://user-images.githubusercontent.com/644763/136678801-bbbfa386-662f-4876-a02c-0e69687e3909.png)

This is supposed to be JSON. I tried the `.json()` call, and passing it to `.body()` as a `String` and as a `Vec<u8>`, all with the exact same result.

I'm pretty sure that this happened in 6d682b53e246f9e982af81697bff0024b9fa82e3. I'm on reqwest version 0.11.5.


Discussion / hints:
Eek, looks bad. We can revert that commit and get another release out. It'd be preferable if we could improve the wasm test situation here, to catch this. But I've mostly relied on others contributing wasm support.

Fix patch:
diff --git a/src/wasm/body.rs b/src/wasm/body.rs
index 9fd72a32b..1749e821e 100644
--- a/src/wasm/body.rs
+++ b/src/wasm/body.rs
@@ -40,8 +40,7 @@ impl Body {
             Inner::Bytes(body_bytes) => {
                 let body_bytes: &[u8] = body_bytes.as_ref();
                 let body_uint8_array: Uint8Array = body_bytes.into();
-                let body_array = Array::new();
-                body_array.push(&body_uint8_array);
+                let body_array = Array::from(&body_uint8_array);
                 let js_value: &JsValue = body_array.as_ref();
                 Ok(js_value.to_owned())
             }
@@ -129,3 +128,49 @@ impl fmt::Debug for Body {
         f.debug_struct("Body").finish()
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use js_sys::{Array, Uint8Array};
+    use wasm_bindgen::{prelude::*, JsValue};
+    use wasm_bindgen_test::*;
+
+    wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
+
+    #[wasm_bindgen]
+    extern "C" {
+        // Use `js_namespace` here to bind `console.log(..)` instead of just
+        // `log(..)`
+        #[wasm_bindgen(js_namespace = console)]
+        fn log(s: String);
+    }
+
+    #[wasm_bindgen_test]
+    async fn test_body() {
+        use crate::Body;
+
+        let body = Body::from("TEST");
+        assert_eq!([84, 69, 83, 84], body.as_bytes().unwrap());
+    }
+
+    #[wasm_bindgen_test]
+    async fn test_body_js() {
+        use crate::Body;
+
+        let body = Body::from("TEST");
+
+        let arr = [84u8, 69, 83, 84];
+        let u8_arr: Uint8Array = arr.as_ref().into();
+        let expected = JsValue::from(Array::from(&u8_arr));
+
+        // ty JS -.-
+        assert_eq!(
+            format!("{:#?}", expected),
+            format!(
+                "{:#?}",
+                body.to_js_value()
+                    .expect("could not convert body to JsValue")
+            )
+        );
+    }
+}


Test patch: