wasm: Add request body in the form of Bytes (#696)
* Add body bytes * Add example and header creation code
This commit is contained in:
committed by
Sean McArthur
parent
b24b0be461
commit
f6f81f9cc1
5
examples/wasm_header/.gitignore
vendored
Normal file
5
examples/wasm_header/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
node_modules
|
||||
pkg
|
||||
target
|
||||
Cargo.lock
|
||||
*.swp
|
||||
16
examples/wasm_header/Cargo.toml
Normal file
16
examples/wasm_header/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "wasm"
|
||||
version = "0.1.0"
|
||||
authors = ["John Gallagher <john.willis.gallagher@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# Config mostly pulled from: https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/Cargo.toml
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
reqwest = {path = "../../"}
|
||||
wasm-bindgen-futures = "0.4.1"
|
||||
wasm-bindgen = { version = "0.2.51", features = ["serde-serialize"] }
|
||||
|
||||
11
examples/wasm_header/README.md
Normal file
11
examples/wasm_header/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
## Example usage of Reqwest from WASM
|
||||
|
||||
You can build the example locally with:
|
||||
|
||||
|
||||
npm run serve
|
||||
|
||||
and then visiting http://localhost:8080 in a browser should run the example!
|
||||
|
||||
|
||||
This example is loosely based off of [this example](https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs), an example usage of `fetch` from `wasm-bindgen`.
|
||||
9
examples/wasm_header/index.js
Normal file
9
examples/wasm_header/index.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const rust = import('./pkg');
|
||||
|
||||
rust
|
||||
.then(m => {
|
||||
return m.run().then((data) => {
|
||||
console.log(data);
|
||||
})
|
||||
})
|
||||
.catch(console.error);
|
||||
6101
examples/wasm_header/package-lock.json
generated
Normal file
6101
examples/wasm_header/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
examples/wasm_header/package.json
Normal file
14
examples/wasm_header/package.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"scripts": {
|
||||
"build": "webpack",
|
||||
"serve": "webpack-dev-server"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@wasm-tool/wasm-pack-plugin": "1.0.1",
|
||||
"text-encoding": "^0.7.0",
|
||||
"html-webpack-plugin": "^3.2.0",
|
||||
"webpack": "^4.29.4",
|
||||
"webpack-cli": "^3.1.1",
|
||||
"webpack-dev-server": "^3.1.0"
|
||||
}
|
||||
}
|
||||
23
examples/wasm_header/src/lib.rs
Normal file
23
examples/wasm_header/src/lib.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
// NOTE: This test is a clone of https://github.com/rustwasm/wasm-bindgen/blob/master/examples/fetch/src/lib.rs
|
||||
// but uses Reqwest instead of the web_sys fetch api directly
|
||||
|
||||
/**
|
||||
* curl --location --request POST "https://postman-echo.com/post" \
|
||||
--data "This is expected to be sent back as part of response body."
|
||||
*/
|
||||
#[wasm_bindgen]
|
||||
pub async fn run() -> Result<JsValue, JsValue> {
|
||||
let res = reqwest::Client::new()
|
||||
.post("https://postman-echo.com/post")
|
||||
.body("This is expected to be sent back as part of response body.")
|
||||
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||
// .header("Access-Control-Allow-Origin", "*")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
let text = res.text().await?;
|
||||
|
||||
Ok(JsValue::from_str(&text))
|
||||
}
|
||||
25
examples/wasm_header/webpack.config.js
Normal file
25
examples/wasm_header/webpack.config.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const path = require('path');
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin');
|
||||
const webpack = require('webpack');
|
||||
const WasmPackPlugin = require("@wasm-tool/wasm-pack-plugin");
|
||||
|
||||
module.exports = {
|
||||
entry: './index.js',
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'dist'),
|
||||
filename: 'index.js',
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin(),
|
||||
new WasmPackPlugin({
|
||||
crateDirectory: path.resolve(__dirname, ".")
|
||||
}),
|
||||
// Have this example work in Edge which doesn't ship `TextEncoder` or
|
||||
// `TextDecoder` at this time.
|
||||
new webpack.ProvidePlugin({
|
||||
TextDecoder: ['text-encoding', 'TextDecoder'],
|
||||
TextEncoder: ['text-encoding', 'TextEncoder']
|
||||
})
|
||||
],
|
||||
mode: 'development'
|
||||
};
|
||||
Reference in New Issue
Block a user