Skip to content

Commit 76a0733

Browse files
committed
use content-length driven Body::len in http_get.
1 parent 8eef32e commit 76a0733

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

examples/http_get.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::error::Error;
2-
use wstd::http::{Client, HeaderValue, Method, Request};
2+
use wstd::http::{Body, Client, HeaderValue, Method, Request};
33
use wstd::io::AsyncRead;
44

55
#[wstd::main]
@@ -17,8 +17,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
1717
.ok_or_else(|| "response expected to have Content-Type header")?;
1818
assert_eq!(content_type, "application/json; charset=utf-8");
1919

20+
let body = response.body();
21+
let body_len = body
22+
.len()
23+
.ok_or_else(|| "GET postman-echo.com/get is supposed to provide a content-length")?;
24+
2025
let mut body_buf = Vec::new();
21-
let _body_len = response.body().read_to_end(&mut body_buf).await?;
26+
body.read_to_end(&mut body_buf).await?;
27+
28+
assert_eq!(
29+
body_buf.len(),
30+
body_len,
31+
"read_to_end length should match content-length"
32+
);
2233

2334
let val: serde_json::Value = serde_json::from_slice(&body_buf)?;
2435
let body_url = val

0 commit comments

Comments
 (0)