use std::io::{self, Write};
use std::sync::{Arc, Mutex};
use hyper::{Chunk, Client, Error, Uri};
use hyper::rt::{self, Future, Stream};
pub fn run() {
let fut = do_request().map(move |body|{
println!("{}", body);
}).map_err(|err|{
println!("{:?}", err);
});
rt::run(fut);
// println!("{:?}", body);
println!("lalala");
println!("lalala");
println!("lalala");
println!("lalala")
}
fn do_request() -> impl Future<Item=String, Error=hyper::error::Error> {
let client = Client::new();
client
.get(Uri::from_static("http://httpbin.org/ip"))
.and_then(|res| {
println!("status: {}", res.status());
res.into_body().concat2()
})
.and_then(|body| {
let s = ::std::str::from_utf8(&body).expect("httpbin sends utf-8 JSON");
Ok(s.to_string())
})
.map_err(|err| {
err
})
}
想在外面获取闭包里面的body数据,请问该如何做?
1
共 2 条评论, 1 页
评论区
写评论返回的 future,可以继续 and_then