Skip to content

Commit 23610fa

Browse files
juntaokelvinparmar
authored andcommitted
Update client.md for HTTPS in reqwest (WasmEdge#202)
* Update client.md for HTTPS in reqwest Signed-off-by: Michael Yuan <michael@secondstate.io> * Update my_sql_driver.md Signed-off-by: Michael Yuan <michael@secondstate.io> --------- Signed-off-by: Michael Yuan <michael@secondstate.io> Signed-off-by: kelvin <pkelvin123456789@gmail.com>
1 parent b21a2f9 commit 23610fa

File tree

2 files changed

+94
-66
lines changed

2 files changed

+94
-66
lines changed

docs/develop/rust/database/my_sql_driver.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The database connection is necessary for today's enterprise development. WasmEdg
88

99
<!-- prettier-ignore -->
1010
:::note
11-
Before we start, ensure [you have Rust and WasmEdge installed](../setup.md).
11+
Before we start, ensure [you have Rust and WasmEdge installed](../setup.md). If you are connecting to a remote MySQL database using TLS, you will need to [install the TLS plugin](../../../start/install.md#tls-plug-in) for WasmEdge as well.
1212
:::
1313

1414
## Run the example
@@ -26,6 +26,19 @@ cargo build --target wasm32-wasi --release
2626
wasmedge --env "DATABASE_URL=mysql://user:passwd@127.0.0.1:3306/mysql" target/wasm32-wasi/release/crud.wasm
2727
```
2828

29+
To use TLS, you will need to turn on the `default-rustls` feature in `Cargo.toml`.
30+
31+
```
32+
mysql_async_wasi = { version = "0.31", features = [ "default-rustls" ] }
33+
```
34+
35+
Then, run the application as follows.
36+
37+
```
38+
# Execute MySQL statements against an AWS RDS database that requires TLS
39+
wasmedge --env "DATABASE_SSL=1" --env "DATABASE_URL=mysql://user:passwd@mydb.123456789012.us-east-1.rds.amazonaws.com:3306/mysql" crud.wasm
40+
```
41+
2942
## Code explanation
3043

3144
<!-- prettier-ignore -->

docs/develop/rust/http_service/client.md

Lines changed: 80 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,95 @@ Before we start, ensure [you have Rust and WasmEdge installed](../setup.md). To
1313

1414
We will discuss HTTP and HTTPS clients using popular Rust APIs.
1515

16-
- [The hyper API (recommended)](#the-hyper-api)
17-
- [The http_req API](#the-http_req-api)
1816
- [The reqwest API](#the-reqwest-api)
17+
- [The hyper API](#the-hyper-api)
18+
- [The http_req API](#the-http_req-api)
1919

20-
## The hyper API
20+
## The reqwest API
2121

22-
The [hyper crate](https://crates.io/crates/hyper) is a widely used Rust library to create HTTP and HTTPS networking applications. We recommend you use it in WasmEdge applications. A key feature of the `hyper` crate is that it is based on the `tokio` runtime, which supports asynchronous network connections. Asynchronous HTTP or HTTPS requests do not block the execution of the calling application. It allows an application to make multiple concurrent HTTP requests and to process responses as they are received. That enables high-performance networking applications in WasmEdge.
22+
The `reqwest` crate is a popular Rust library to create asynchronous HTTP clients. It is built on top of the `hyper` and `tokio` APIs. Many developers find it easier to use. But perhaps more importantly, many existing Rust applications use `reqwest`, and you can make them work in WasmEdge by simply replacing the `reqwest` crate with `reqwest_wasi`! Build and run [the example](https://github.com/WasmEdge/wasmedge_reqwest_demo/) in WasmEdge as follows.
2323

2424
<!-- prettier-ignore -->
2525
:::note
2626
Non-blocking I/O means that the application program can keep multiple connections open simultaneously, and process data in and out of those connections as they come in. The program can either alternatingly poll those open connections or wait for incoming data to trigger async functions. That allows I/O intensive programs to run much faster, even in a single-threaded environment.
2727
:::
2828

29-
Build and run [the hyper example](https://github.com/WasmEdge/wasmedge_hyper_demo/) in WasmEdge as follows.
29+
```bash
30+
git clone https://github.com/WasmEdge/wasmedge_reqwest_demo
31+
cd wasmedge_reqwest_demo
32+
33+
# Build the Rust code
34+
cargo build --target wasm32-wasi --release
35+
# Use the AoT compiler to get better performance
36+
wasmedge compile target/wasm32-wasi/release/http.wasm http.wasm
37+
wasmedge compile target/wasm32-wasi/release/https.wasm https.wasm
38+
39+
# Run the HTTP GET and POST examples
40+
wasmedge http.wasm
41+
42+
# Run the HTTPS GET and POST examples
43+
wasmedge https.wasm
44+
```
45+
46+
In your Rust application, import [the WasmEdge adapted reqwest crate](https://crates.io/crates/reqwest_wasi), which uses a special version of single-threaded Tokio that is adapted for WebAssembly. Just add the following lines to your `Cargo.toml`.
47+
48+
```toml
49+
[dependencies]
50+
reqwest_wasi = { version = "0.11", features = ["json"] }
51+
tokio_wasi = { version = "1.21", features = ["full"] }
52+
```
53+
54+
The [example Rust code](https://github.com/WasmEdge/wasmedge_reqwest_demo/blob/main/src/http.rs) below shows an HTTP GET request.
55+
56+
```rust
57+
use std::collections::HashMap;
58+
59+
#[tokio::main]
60+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
61+
let resp = reqwest::get("http://eu.httpbin.org/ip")
62+
.await?
63+
.json::<HashMap<String, String>>()
64+
.await?;
65+
println!("{:#?}", resp);
66+
Ok(())
67+
}
68+
```
69+
70+
And here is an HTTP POST request.
71+
72+
```rust
73+
let client = reqwest::Client::new();
74+
75+
let res = client
76+
.post("http://eu.httpbin.org/post")
77+
.body("msg=WasmEdge")
78+
.send()
79+
.await?;
80+
let body = res.text().await?;
81+
82+
println!("POST: {}", body);
83+
```
84+
85+
### HTTPS support
86+
87+
In order to make HTTPS requests from `reqwest`, you will need to install WasmEdge with [the TLS plug-in](../../../start/install.md#tls-plug-in).
88+
89+
```
90+
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | bash -s -- --plugins wasmedge_rustls
91+
```
92+
93+
Then, in `Cargo.toml`, you should enable the TLS feature.
94+
95+
```
96+
reqwest_wasi = { version = "0.11", features = ["wasmedge-tls"] }
97+
tokio_wasi = { version = "1", features = ["rt", "macros", "net", "time"] }
98+
```
99+
100+
Then, you can just replace the URLs in the above examples from `http` and `https`. [See details here](https://github.com/WasmEdge/wasmedge_reqwest_demo/blob/main/src/https.rs).
101+
102+
## The hyper API
103+
104+
The [hyper crate](https://crates.io/crates/hyper) is a widely used Rust library to create HTTP and HTTPS networking applications for both clients and servers. A key feature of the `hyper` crate is that it is based on the `tokio` runtime, which supports asynchronous network connections. Asynchronous HTTP or HTTPS requests do not block the execution of the calling application. It allows an application to make multiple concurrent HTTP requests and to process responses as they are received. That enables high-performance networking applications in WasmEdge. Build and run [the hyper example](https://github.com/WasmEdge/wasmedge_hyper_demo/) in WasmEdge as follows.
30105

31106
```bash
32107
git clone https://github.com/WasmEdge/wasmedge_hyper_demo
@@ -171,63 +246,3 @@ fn main() {
171246
}
172247
```
173248

174-
## The reqwest API
175-
176-
The `reqwest` crate is another popular Rust library to create asynchronous HTTP clients. It is built on top of the `hyper` API. Many developers find it easier to use. But perhaps more importantly, many existing Rust applications use `reqwest`, and you can make them work in WasmEdge by simply replacing the `reqwest` crate with `reqwest_wasi`! Build and run [the example](https://github.com/WasmEdge/wasmedge_reqwest_demo/) in WasmEdge as follows.
177-
178-
<!-- prettier-ignore -->
179-
:::note
180-
Our current adaptation of [reqwest_wasi](https://github.com/wasmedge/reqwest) does not support HTTPS yet. You are welcome to contribute to the project!
181-
:::
182-
183-
```bash
184-
git clone https://github.com/WasmEdge/wasmedge_reqwest_demo
185-
cd wasmedge_reqwest_demo
186-
187-
# Build the Rust code
188-
cargo build --target wasm32-wasi --release
189-
# Use the AoT compiler to get better performance
190-
wasmedge compile target/wasm32-wasi/release/wasmedge_reqwest_demo.wasm wasmedge_reqwest_demo.wasm
191-
192-
# Run the example
193-
wasmedge wasmedge_reqwest_demo.wasm
194-
```
195-
196-
In your Rust application, import [the WasmEdge adapted reqwest crate](https://crates.io/crates/reqwest_wasi), which uses a special version of single-threaded Tokio that is adapted for WebAssembly. Just add the following lines to your `Cargo.toml`.
197-
198-
```toml
199-
[dependencies]
200-
reqwest_wasi = { version = "0.11", features = ["json"] }
201-
tokio_wasi = { version = "1.21", features = ["full"] }
202-
```
203-
204-
The [example Rust code](https://github.com/WasmEdge/wasmedge_reqwest_demo/blob/main/src/main.rs) below shows an HTTP GET request.
205-
206-
```rust
207-
use std::collections::HashMap;
208-
209-
#[tokio::main]
210-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
211-
let resp = reqwest::get("http://eu.httpbin.org/ip")
212-
.await?
213-
.json::<HashMap<String, String>>()
214-
.await?;
215-
println!("{:#?}", resp);
216-
Ok(())
217-
}
218-
```
219-
220-
And here is an HTTP POST request.
221-
222-
```rust
223-
let client = reqwest::Client::new();
224-
225-
let res = client
226-
.post("http://eu.httpbin.org/post")
227-
.body("msg=WasmEdge")
228-
.send()
229-
.await?;
230-
let body = res.text().await?;
231-
232-
println!("POST: {}", body);
233-
```

0 commit comments

Comments
 (0)