|
| 1 | +use async_std::io::prelude::*; |
1 | 2 | use async_std::io::{self, BufRead, Read};
|
2 | 3 |
|
3 | 4 | use std::fmt::{self, Debug};
|
@@ -81,7 +82,7 @@ impl Body {
|
81 | 82 |
|
82 | 83 | /// Create a `Body` from a reader with an optional length.
|
83 | 84 | ///
|
84 |
| - /// The Mime type set to `application/octet-stream` if no other mime type has been set or can |
| 85 | + /// The Mime type is set to `application/octet-stream` if no other mime type has been set or can |
85 | 86 | /// be sniffed. If a `Body` has no length, HTTP implementations will often switch over to
|
86 | 87 | /// framed messages such as [Chunked
|
87 | 88 | /// Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding).
|
@@ -109,6 +110,53 @@ impl Body {
|
109 | 110 | }
|
110 | 111 | }
|
111 | 112 |
|
| 113 | + /// Create a `Body` from a Vec of bytes. |
| 114 | + /// |
| 115 | + /// The Mime type is set to `application/octet-stream` if no other mime type has been set or can |
| 116 | + /// be sniffed. If a `Body` has no length, HTTP implementations will often switch over to |
| 117 | + /// framed messages such as [Chunked |
| 118 | + /// Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Transfer-Encoding). |
| 119 | + /// |
| 120 | + /// # Examples |
| 121 | + /// |
| 122 | + /// ``` |
| 123 | + /// use http_types::{Body, Response, StatusCode}; |
| 124 | + /// use async_std::io::Cursor; |
| 125 | + /// |
| 126 | + /// let mut req = Response::new(StatusCode::Ok); |
| 127 | + /// |
| 128 | + /// let input = vec![1, 2, 3]; |
| 129 | + /// req.set_body(Body::from_bytes(input)); |
| 130 | + /// ``` |
| 131 | + pub fn from_bytes(bytes: Vec<u8>) -> Self { |
| 132 | + Self { |
| 133 | + mime: mime::BYTE_STREAM, |
| 134 | + length: Some(bytes.len()), |
| 135 | + reader: Box::new(io::Cursor::new(bytes)), |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /// Parse the body into a `Vec<u8>`. |
| 140 | + /// |
| 141 | + /// # Examples |
| 142 | + /// |
| 143 | + /// ``` |
| 144 | + /// # fn main() -> Result<(), http_types::Error> { async_std::task::block_on(async { |
| 145 | + /// use http_types::Body; |
| 146 | + /// |
| 147 | + /// let bytes = vec![1, 2, 3]; |
| 148 | + /// let body = Body::from_bytes(bytes); |
| 149 | + /// |
| 150 | + /// let bytes: Vec<u8> = body.into_bytes().await?; |
| 151 | + /// assert_eq!(bytes, vec![1, 2, 3]); |
| 152 | + /// # Ok(()) }) } |
| 153 | + /// ``` |
| 154 | + pub async fn into_bytes(mut self) -> crate::Result<Vec<u8>> { |
| 155 | + let mut buf = Vec::with_capacity(1024); |
| 156 | + self.read_to_end(&mut buf).await?; |
| 157 | + Ok(buf) |
| 158 | + } |
| 159 | + |
112 | 160 | /// Get the length of the body in bytes.
|
113 | 161 | ///
|
114 | 162 | /// # Examples
|
@@ -165,7 +213,6 @@ impl Body {
|
165 | 213 | /// # Ok(()) }) }
|
166 | 214 | /// ```
|
167 | 215 | pub async fn into_string(mut self) -> io::Result<String> {
|
168 |
| - use async_std::io::ReadExt; |
169 | 216 | let mut result = String::with_capacity(self.len().unwrap_or(0));
|
170 | 217 | self.read_to_string(&mut result).await?;
|
171 | 218 | Ok(result)
|
|
0 commit comments