BREAKING CHANGE: `Method`, `Request`, `Response`, `StatusCode`, `Version`, and `Uri` have been replaced with types from the `http` crate. The `hyper::header` module is gone for now. Removed `Client::get`, since it needed to construct a `Request<B>` with an empty body. Just use `Client::request` instead. Removed `compat` cargo feature, and `compat` related API.
52 lines
1.2 KiB
Rust
52 lines
1.2 KiB
Rust
//#![deny(warnings)]
|
|
extern crate futures;
|
|
extern crate hyper;
|
|
extern crate tokio_core;
|
|
|
|
extern crate pretty_env_logger;
|
|
|
|
use std::env;
|
|
use std::io::{self, Write};
|
|
|
|
use futures::Future;
|
|
use futures::stream::Stream;
|
|
|
|
use hyper::{Body, Client, Request};
|
|
|
|
fn main() {
|
|
pretty_env_logger::init();
|
|
|
|
let url = match env::args().nth(1) {
|
|
Some(url) => url,
|
|
None => {
|
|
println!("Usage: client <url>");
|
|
return;
|
|
}
|
|
};
|
|
|
|
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.");
|
|
return;
|
|
}
|
|
|
|
let mut core = tokio_core::reactor::Core::new().unwrap();
|
|
let handle = core.handle();
|
|
let client = Client::new(&handle);
|
|
|
|
let mut req = Request::new(Body::empty());
|
|
*req.uri_mut() = url;
|
|
let work = client.request(req).and_then(|res| {
|
|
println!("Response: {}", res.status());
|
|
println!("Headers: {:#?}", res.headers());
|
|
|
|
res.into_parts().1.for_each(|chunk| {
|
|
io::stdout().write_all(&chunk).map_err(From::from)
|
|
})
|
|
}).map(|_| {
|
|
println!("\n\nDone.");
|
|
});
|
|
|
|
core.run(work).unwrap();
|
|
}
|