You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/develop/rust/http_service/client.md
+54-87
Original file line number
Diff line number
Diff line change
@@ -8,18 +8,17 @@ WasmEdge allows Rust developers to use APIs they are already familiar with to ac
8
8
9
9
<!-- prettier-ignore -->
10
10
:::note
11
-
Before we start, ensure [you have Rust and WasmEdge installed](../setup.md). To make HTTPS requests, install the [WasmEdge TLS plug-in](../../../start/install.md#tls-plug-in).
11
+
Before we start, ensure [you have Rust and WasmEdge installed](../setup.md).
12
12
:::
13
13
14
14
We will discuss HTTP and HTTPS clients using popular Rust APIs.
15
15
16
16
-[The reqwest API](#the-reqwest-api)
17
17
-[The hyper API](#the-hyper-api)
18
-
-[The http_req API](#the-http_req-api)
19
18
20
19
## The reqwest API
21
20
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.
21
+
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 patching the `reqwest` crate in `Cargo.toml` with simple patches! Build and run [the example](https://github.com/WasmEdge/wasmedge_reqwest_demo/) in WasmEdge as follows.
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`.
45
+
In your Rust application, import the standard [reqwest](https://crates.io/crates/reqwest) and [tokio](https://crates.io/crates/tokio) crates. You will also patch a few dependency crates to make them aware of the WasmEdge socket API. Just add the following lines to your `Cargo.toml`.
47
46
48
47
```toml
48
+
[patch.crates-io]
49
+
tokio = { git = "https://github.com/second-state/wasi_tokio.git", branch = "v1.36.x" }
reqwest_wasi = { version = "0.11", features = ["json"] }
51
-
tokio_wasi = { version = "1.21", features = ["full"] }
55
+
reqwest = { version = "0.11", default-features = false, features = ["rustls-tls"] }
56
+
tokio = { version = "1", features = ["rt", "macros", "net", "time"] }
52
57
```
53
58
59
+
<!-- prettier-ignore -->
60
+
:::note
61
+
The `Cargo.toml` here shows that TLS is enabled. If you need to compile it on the MacOS, you will need the [wasi-sdk version of clang](../setup#compile-rust-tls-on-macos).
62
+
:::
63
+
54
64
The [example Rust code](https://github.com/WasmEdge/wasmedge_reqwest_demo/blob/main/src/http.rs) below shows an HTTP GET request.
55
65
56
66
```rust
@@ -82,23 +92,6 @@ And here is an HTTP POST request.
82
92
println!("POST: {}", body);
83
93
```
84
94
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).
Then, in `Cargo.toml`, you should enable the TLS feature.
94
-
95
-
```toml
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
95
## The hyper API
103
96
104
97
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.
In your Rust application, import [the WasmEdge adapted hyper crate](https://crates.io/crates/hyper_wasi), which uses a special version of single-threaded Tokio that is adapted for WebAssembly. Just add the following line to your `Cargo.toml`.
139
+
In the HTTPS version of `Cargo.toml`, you just need to import the standard [hyper-rustls](https://crates.io/crates/hyper-rustls), [rustls](https://crates.io/crates/rustls) and [webpki-roots](https://crates.io/crates/webpki-roots) crates with the same patches as above.
140
+
141
+
```
142
+
[patch.crates-io]
143
+
tokio = { git = "https://github.com/second-state/wasi_tokio.git", branch = "v1.36.x" }
hyper-rustls = { version = "0.25", default-features = false, features = [ "http1", "tls12", "logging", "ring", "webpki-tokio" ] }
150
+
rustls = { version = "0.22", default-features = false }
151
+
webpki-roots = "0.26.1"
152
+
153
+
tokio = { version = "1", features = ["rt", "macros", "net", "time", "io-util"]}
154
+
pretty_env_logger = "0.4.0"
136
155
```
137
156
157
+
<!-- prettier-ignore -->
158
+
:::note
159
+
If you need to compile `rustls` as shown in the `Cargo.toml` above on the MacOS, you will need the [wasi-sdk version of clang](../setup#compile-rust-tls-on-macos).
160
+
:::
161
+
138
162
The [Rust example code](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/client/src/main.rs) below shows an HTTP GET request.
If your WasmEdge application only needs to make sequential requests to external web services, a synchronous client is easier to work with. It allows you to make a request, wait for the response, and move on once the response is fully received. Use the `http_req` API to make simple synchronous HTTP requests. Build and run [the example](https://github.com/second-state/http_req/) in WasmEdge.
In your Rust application, import the [http_req_wasi](https://crates.io/crates/http_req_wasi) crate, which is compatible with [http_req](https://github.com/jayjamesjay/http_req) at the API level. Just add the following line to your `Cargo.toml`.
212
-
213
-
```toml
214
-
[dependencies]
215
-
http_req_wasi = "0.10"
216
-
```
217
-
218
-
The example below shows an [HTTP GET request](https://github.com/second-state/http_req/blob/master/examples/get.rs). For HTTPS requests, you can [simply change](https://github.com/second-state/http_req/blob/master/examples/get_https.rs) the `http` URL to `https`.
219
-
220
-
```rust
221
-
usehttp_req::request;
222
-
223
-
fnmain() {
224
-
letmutwriter=Vec::new(); //container for body of a response
And here is an [HTTP POST request](https://github.com/second-state/http_req/blob/master/examples/post.rs). For HTTPS requests, you can [simply change](https://github.com/second-state/http_req/blob/master/examples/post_https.rs) the `http` URL to `https`.
234
-
235
-
```rust
236
-
usehttp_req::request;
237
-
238
-
fnmain() {
239
-
letmutwriter=Vec::new(); //container for body of a response
Copy file name to clipboardexpand all lines: docs/develop/rust/http_service/server.md
+10-5
Original file line number
Diff line number
Diff line change
@@ -71,14 +71,14 @@ async fn main() {
71
71
72
72
## The hyper API
73
73
74
-
The `warp` crate is convenient to use. But oftentimes, developers need access to lower-level APIs. The `hyper` crate is an excellent HTTP library for that. Build and run [the example](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/server/) in WasmEdge as follows.
74
+
The `hyper` crate is an excellent library for building HTTP servers using customizable low level APIs. Build and run [the example](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/server/) in WasmEdge as follows.
In your Rust application, import the WasmEdge adapted `hyper_wasi` crate, which uses a special version of single threaded Tokio that is adapted for WebAssembly. Just add the following lines to your `Cargo.toml`.
96
+
In your Rust application, import the [hyper](https://crates.io/crates/hyper) and [tokio](https://crates.io/crates/tokio) crates, as well as the WasmEdge patches. Just add the following lines to your `Cargo.toml`.
97
97
98
98
```toml
99
+
[patch.crates-io]
100
+
tokio = { git = "https://github.com/second-state/wasi_tokio.git", branch = "v1.36.x" }
tokio_wasi = { version = "1", features = ["rt", "macros", "net", "time", "io-util"]}
101
-
hyper_wasi = "0.15.0"
105
+
hyper = { version = "0.14", features = ["full"]}
106
+
tokio = { version = "1", features = ["rt", "macros", "net", "time", "io-util"]}
102
107
```
103
108
104
109
The [Rust example code](https://github.com/WasmEdge/wasmedge_hyper_demo/blob/main/server/src/main.rs) below shows an HTTP server that echoes back any incoming request.
0 commit comments