66 lines (56 sloc) 1.94 KB
extern crate hyper;
extern crate pretty_env_logger;
use std::env;
use std::io::{self, Write};
use hyper::Client;
use hyper::rt::{self, Future, Stream};
fn main() {
// Some simple CLI args requirements...
let url = match env::args().nth(1) {
Some(url) => url,
None => {
println!("Usage: client <url>");
// 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_part().map(|s| s.as_ref()) != Some("http") {
println!("This example only works with 'http' URLs.");
// Run the runtime with the future trying to fetch and print this URL.
// Note that in more complicated use cases, the runtime should probably
// run on its own, and futures should just be spawned into it.
fn fetch_url(url: hyper::Uri) -> impl Future<Item=(), Error=()> {
let client = Client::new();
// Fetch the url...
// And then, if we get a response back...
.and_then(|res| {
println!("Response: {}", res.status());
println!("Headers: {:#?}", res.headers());
// The body is a stream, and for_each returns a new Future
// when the stream is finished, and calls the closure on
// each chunk of the body...
res.into_body().for_each(|chunk| {
.map_err(|e| panic!("example expects stdout is open, error={}", e))
// If all good, just tell the user...
.map(|_| {
// If there was an error, let the user know...
.map_err(|err| {
eprintln!("Error {}", err);