Skip to content

Commit

Permalink
Add https example
Browse files Browse the repository at this point in the history
Signed-off-by: csh <[email protected]>
  • Loading branch information
L-jasmine committed Nov 20, 2023
1 parent 19a88b0 commit b7c92f3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,27 @@ jobs:
run: |
VERSION=0.13.5
curl -sSf https://raw.githubusercontent.com/WasmEdge/WasmEdge/master/utils/install.sh | sudo bash -s -- --version=$VERSION -p /usr/local
mkdir -p /usr/local/lib/wasmedge
wget https://github.com/WasmEdge/WasmEdge/releases/download/$VERSION/WasmEdge-plugin-wasmedge_rustls-$VERSION-ubuntu20.04_x86_64.tar.gz
tar -zxf WasmEdge-plugin-wasmedge_rustls-$VERSION-ubuntu20.04_x86_64.tar.gz
sudo mv libwasmedge_rustls.so /usr/local/lib/wasmedge
- name: Client example
run: |
cargo build --target wasm32-wasi --release
wasmedgec target/wasm32-wasi/release/wasmedge_reqwest_demo.wasm wasmedge_reqwest_demo.wasm
resp=$(wasmedge wasmedge_reqwest_demo.wasm 2>&1)
wasmedgec target/wasm32-wasi/release/http.wasm http.wasm
wasmedgec target/wasm32-wasi/release/https.wasm https.wasm
resp=$(wasmedge http.wasm 2>&1)
echo "$resp"
if [[ $resp == *"WasmEdge"* ]]; then
echo -e "Execution Success!"
else
echo -e "Execution Fail!"
exit 1
fi
resp=$(wasmedge https.wasm 2>&1)
echo "$resp"
if [[ $resp == *"WasmEdge"* ]]; then
echo -e "Execution Success!"
Expand Down
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,13 @@ version = "0.1.0"
edition = "2021"

[dependencies]
reqwest_wasi = "0.11"
tokio_wasi = { version = "1", features = ["rt", "macros", "net", "time"]}
reqwest_wasi = { version = "0.11", features = ["wasmedge-tls"] }
tokio_wasi = { version = "1", features = ["rt", "macros", "net", "time"] }

[[bin]]
name = "http"
path = "src/http.rs"

[[bin]]
name = "https"
path = "src/https.rs"
File renamed without changes.
37 changes: 37 additions & 0 deletions src/https.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), reqwest::Error> {
// Some simple CLI args requirements...
let url = "https://eu.httpbin.org/get?msg=WasmEdge";

eprintln!("Fetching {:?}...", url);

let res = reqwest::get(url).await?;

eprintln!("Response: {:?} {}", res.version(), res.status());
eprintln!("Headers: {:#?}\n", res.headers());

let body = res.text().await?;
println!("GET: {}", body);

let client = reqwest::Client::new();

let res = client
.post("https://eu.httpbin.org/post")
.body("msg=WasmEdge")
.send()
.await?;
let body = res.text().await?;

println!("POST: {}", body);

let res = client
.put("https://eu.httpbin.org/put")
.body("msg=WasmEdge")
.send()
.await?;
let body = res.text().await?;

println!("PUT: {}", body);

Ok(())
}

0 comments on commit b7c92f3

Please sign in to comment.