Can't run hyper http client example

I'm trying to test the hyper crate which is an http client. I simply pasted the example from the github:

#![deny(warnings)]
#![warn(rust_2018_idioms)]
use std::env;

use hyper::{body::HttpBody as _, Client};
use tokio::io::{self, AsyncWriteExt as _};

// A simple type alias so as to DRY.
type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>>;

#[tokio::main]
async fn main() -> Result<()> {
    pretty_env_logger::init();

    // Some simple CLI args requirements...
    let url = match env::args().nth(1) {
        Some(url) => url,
        None => {
            println!("Usage: client <url>");
            return Ok(());
        }
    };

    // HTTPS requires picking a TLS implementation, so give a better
    // warning if the user tries to request an 'https' URL.
    let url = url.parse::<hyper::Uri>().unwrap();
    if url.scheme_str() != Some("http") {
        println!("This example only works with 'http' URLs.");
        return Ok(());
    }

    fetch_url(url).await
}

async fn fetch_url(url: hyper::Uri) -> Result<()> {
    let client = Client::new();

    let mut res = client.get(url).await?;

    println!("Response: {}", res.status());
    println!("Headers: {:#?}\n", res.headers());

    // Stream the body, writing each chunk to stdout as we get it
    // (instead of buffering and printing at the end).
    while let Some(next) = res.data().await {
        let chunk = next?;
        io::stdout().write_all(&chunk).await?;
    }

    println!("\n\nDone!");

    Ok(())
}

Here's my Cargo.toml:

[package]
name = "hyper_http_custom_transport"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
hyper = "0.14"
tokio = { version = "1", features = ["full"] }

But I'm getting errors:

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
  --> src/main.rs:46:25
   |
46 |         let chunk = next?;
   |                         ^ doesn't have a size known at compile-time

and many others like

error[E0432]: unresolved import `hyper::Client`
 --> src/main.rs:5:34
  |
5 | use hyper::{body::HttpBody as _, Client};
  |                                  ^^^^^^ no `Client` in the root

What is wrong?

You need to enable some features on hyper like you did on Tokio. I believe hyper has a full feature too.

thanks, this worked:

[package]
name = "hyper_http_custom_transport"
version = "0.1.0"
edition = "2018"

[dependencies]
hyper = {version = "0.14", features = ["full"]}
tokio = { version = "1", features = ["full"] }
pretty_env_logger = "0.4.0"

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.