Skip to content

Instantly share code, notes, and snippets.

@pimeys
Created November 2, 2018 14:37
Show Gist options
  • Save pimeys/b1bb7bd29daf842d920254fcc93ecae8 to your computer and use it in GitHub Desktop.
Save pimeys/b1bb7bd29daf842d920254fcc93ecae8 to your computer and use it in GitHub Desktop.
#![feature(await_macro, async_await, futures_api)]
#[macro_use]
extern crate tokio;
extern crate hyper;
use tokio::prelude::*;
use hyper::Client;
use std::time::Duration;
use std::str;
async fn do_request() -> Result<String, hyper::error::Error> {
let client = Client::new();
let uri = "http://httpbin.org/ip".parse().unwrap();
let response = await!(
client
.get(uri)
.timeout(Duration::from_secs(10))
).unwrap();
let body = await!(response.into_body().concat2()).unwrap();
Ok(String::from_utf8(body.to_vec()).unwrap())
}
pub fn main() {
tokio::run_async(async {
match await!(do_request()) {
Ok(body) => println!("{}", body),
Err(e) => println!("ERROR: {:?}", e)
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment