Advertisement
NLinker

Async byte stream from reqwest

Dec 20th, 2021
1,800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.92 KB | None | 0 0
  1. /*
  2. [package]
  3. name = "smol-test"
  4. version = "0.1.0"
  5. edition = "2021"
  6.  
  7. [dependencies]
  8. anyhow = "1.0"
  9. futures = "0.3"
  10. reqwest = { version = "0.11", default-features = false, features = ["stream"] }
  11. tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
  12. */
  13.  
  14. use futures::io::AsyncReadExt;
  15. use futures::TryStreamExt;
  16. use reqwest::{Client, Response};
  17. use std::io::{Error, ErrorKind};
  18. use std::time::Duration;
  19.  
  20. #[tokio::main]
  21. async fn main() -> anyhow::Result<()> {
  22.     let http_client = Client::builder()
  23.         .connect_timeout(Duration::from_secs(5))
  24.         .build()?;
  25.     let r: Response = http_client.get("http://httpbin.org/get").send().await?;
  26.     let mut reader = r
  27.         .bytes_stream()
  28.         .map_err(|e| Error::new(ErrorKind::Other, e))
  29.         .into_async_read();
  30.     let mut buf = Vec::new();
  31.     let _ = reader.read_to_end(&mut buf).await?;
  32.     println!("{:?}", buf);
  33.  
  34.     Ok(())
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement