soebosi
1/13/2018 - 2:37 PM

hyper sample

hyper sample

extern crate futures;
extern crate hyper;
extern crate tokio_core;

use std::io::{self, Write};
use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Core;

fn get(uri_string: &str) -> Result<(), hyper::Error> {
    let mut core = Core::new()?;
    let client = Client::new(&core.handle());
    let uri = uri_string.parse()?;
    let work = client.get(uri).and_then(|res| {
        println!("Response: {}", res.status());
        res.body().for_each(|chunk| {
            io::stdout().write_all(&chunk).map_err(From::from)
        })
    });
    core.run(work)
}

fn main() {
    let uri = "http://httpbin.org/ip";
    if let Err(err) = get(uri) {
        println!("Error: {}", err);
    }
}