< 返回版块

zhaobingss 发表于 2019-07-09 15:45

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数据,请问该如何做?

评论区

写评论
gaxxx 2019-07-10 21:55
  1. channel
  2. Arc<Mutex>
Ryan-Git 2019-07-09 17:48

返回的 future,可以继续 and_then

1 共 2 条评论, 1 页