Skip to content
This repository was archived by the owner on Jun 7, 2024. It is now read-only.

Commit 66798f0

Browse files
committed
Add json method for Response
1 parent 26a9322 commit 66798f0

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ keywords = ["webassembly", "wasm", "wasi"]
1010
repository = "https://github.com/wacker-dev/wasi-http-client"
1111
license = "Apache-2.0"
1212

13+
[package.metadata.docs.rs]
14+
all-features = true
15+
rustdoc-args = ["--cfg", "docsrs"]
16+
1317
[dependencies]
1418
anyhow = "1.0.83"
1519
wasi = "0.13.0"
1620
url = "2.5.0"
21+
serde = { version = "1.0.201", optional = true }
22+
serde_json = { version = "1.0.117", optional = true }
23+
24+
[features]
25+
json = ["dep:serde", "dep:serde_json"]

src/response.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use anyhow::{anyhow, Result};
2+
#[cfg(feature = "json")]
3+
use serde::de::DeserializeOwned;
24
use std::collections::HashMap;
35
use wasi::http::types::{IncomingResponse, StatusCode};
46
use wasi::io::streams::StreamError;
@@ -57,4 +59,15 @@ impl Response {
5759
pub fn body(&self) -> &Vec<u8> {
5860
&self.body
5961
}
62+
63+
/// Deserialize the response body as JSON.
64+
///
65+
/// # Optional
66+
///
67+
/// This requires the `json` feature enabled.
68+
#[cfg(feature = "json")]
69+
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
70+
pub fn json<T: DeserializeOwned>(&self) -> Result<T> {
71+
Ok(serde_json::from_slice(&self.body)?)
72+
}
6073
}

tests/program/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package = "component:wasi-http-client-test-program"
2121
"wasi:random" = { path = "wit/deps/random" }
2222

2323
[dependencies]
24-
wasi-http-client = { path = "../../../wasi-http-client" }
24+
wasi-http-client = { path = "../../../wasi-http-client", features = ["json"] }
2525

2626
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
27+
serde = { version = "1.0.201", features = ["derive"] }

tests/program/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
mod bindings;
33

44
use bindings::exports::wasi::cli::run::Guest;
5+
use serde::Deserialize;
56
use std::time::Duration;
67
use wasi_http_client::Client;
78

89
struct Component;
910

1011
bindings::export!(Component with_types_in bindings);
1112

13+
#[derive(Debug, Deserialize)]
14+
#[allow(dead_code)]
15+
struct Data {
16+
origin: String,
17+
url: String,
18+
}
19+
1220
impl Guest for Component {
1321
fn run() -> Result<(), ()> {
1422
// get with query
@@ -22,6 +30,15 @@ impl Guest for Component {
2230
String::from_utf8_lossy(resp.body())
2331
);
2432

33+
// get with json response
34+
let resp = Client::new().get("https://httpbin.org/get").send().unwrap();
35+
let json_data = resp.json::<Data>().unwrap();
36+
println!(
37+
"GET https://httpbin.org/get, status code: {}, body:\n{:?}\n",
38+
resp.status(),
39+
json_data,
40+
);
41+
2542
// post with json data
2643
let resp = Client::new()
2744
.post("https://httpbin.org/post")

0 commit comments

Comments
 (0)