File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,36 @@ use super::HttpBody;
13
13
/// Care needs to be taken if the remote is untrusted. The function doesn't implement any length
14
14
/// checks and an malicious peer might make it consume arbitrary amounts of memory. Checking the
15
15
/// `Content-Length` is a possibility, but it is not strictly mandated to be present.
16
+ ///
17
+ /// # Example
18
+ ///
19
+ /// ```
20
+ /// # async fn doc() -> hyper::Result<()> {
21
+ /// use hyper::{body::HttpBody};
22
+ ///
23
+ /// # let request = hyper::Request::builder()
24
+ /// # .method(hyper::Method::POST)
25
+ /// # .uri("http://httpbin.org/post")
26
+ /// # .header("content-type", "application/json")
27
+ /// # .body(hyper::Body::from(r#"{"library":"hyper"}"#)).unwrap();
28
+ /// # let client = hyper::Client::new();
29
+ /// let response = client.request(request).await?;
30
+ ///
31
+ /// const MAX_ALLOWED_RESPONSE_SIZE: u64 = 1024;
32
+ ///
33
+ /// let response_content_length = match response.body().size_hint().upper() {
34
+ /// Some(v) => v,
35
+ /// None => MAX_ALLOWED_RESPONSE_SIZE + 1 // Just to protect ourselves from a malicious response
36
+ /// };
37
+ ///
38
+ /// if response_content_length < MAX_ALLOWED_RESPONSE_SIZE {
39
+ /// let body_bytes = hyper::body::to_bytes(response.into_body()).await?;
40
+ /// println!("body: {:?}", body_bytes);
41
+ /// }
42
+ ///
43
+ /// # Ok(())
44
+ /// # }
45
+ /// ```
16
46
pub async fn to_bytes < T > ( body : T ) -> Result < Bytes , T :: Error >
17
47
where
18
48
T : HttpBody ,
You can’t perform that action at this time.
0 commit comments