NLinker icon

Async byte stream from reqwest

NLinker | PRO | 12/20/21 10:39:33 AM UTC | 0 ⭐ | 8998 👁️ | Never ⏰ | []
Rust |

920 B

|

None

|

0 👍

/

0 👎

/*
[package]
name = "smol-test"
version = "0.1.0"
edition = "2021"
 
[dependencies]
anyhow = "1.0"
futures = "0.3"
reqwest = { version = "0.11", default-features = false, features = ["stream"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
*/
 
use futures::io::AsyncReadExt;
use futures::TryStreamExt;
use reqwest::{Client, Response};
use std::io::{Error, ErrorKind};
use std::time::Duration;
 
#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let http_client = Client::builder()
        .connect_timeout(Duration::from_secs(5))
        .build()?;
    let r: Response = http_client.get("http://httpbin.org/get").send().await?;
    let mut reader = r
        .bytes_stream()
        .map_err(|e| Error::new(ErrorKind::Other, e))
        .into_async_read();
    let mut buf = Vec::new();
    let _ = reader.read_to_end(&mut buf).await?;
    println!("{:?}", buf);
 
    Ok(())
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎