Created
February 23, 2019 07:34
-
-
Save amol9/f660d163ea99b47ef561849d63fa6b41 to your computer and use it in GitHub Desktop.
Rust and http using hyper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate hyper; | |
use std::io::{self, Write}; | |
use hyper::Client; | |
use hyper::rt::{self, Future, Stream}; | |
fn main() { | |
rt::run(rt::lazy(|| { | |
let client = Client::new(); | |
let uri = "http://httpbin.org/ip".parse().unwrap(); | |
client | |
.get(uri) | |
.and_then(|res| { | |
println!("Response: {}", res.status()); | |
res | |
.into_body() | |
.for_each(|chunk| { | |
io::stdout() | |
.write_all(&chunk) | |
.map_err(|e| { | |
panic!("example expects stdout is open, error={}", e) | |
}) | |
}) | |
}) | |
.map_err(|err| { | |
println!("Error: {}", err); | |
}) | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment