Can't read a simple payload making HTTP request via hyper::client::Client: the trait bound `Body: Future` is not satisfied

浪尽此生 提交于 2020-01-15 12:26:05

问题


I am attempting to convert a Result to a Buffer:

let ufc_root: String =
    String::from("https://www.ufc.com/athletes/all?filters%5B0%5D=status%3A23");
// let ufc_root: String = String::from("https://www.google.com");
let https = HttpsConnector::new(4).unwrap();
let client = Client::builder().build::<_, hyper::Body>(https);

client
    .get(ufc_root.parse::<hyper::Uri>().unwrap())
    .and_then(|res| {
        println!("http status code: {}", res.status());
        println!("http response headers:\n{:?}: ", res.headers());
        res.into_body()
    })
    .from_err::<WebScrapeError>()
    .and_then(|body| {
        body.for_each(|chunk| {
            println!("{}", chunk.into_bytes());
        });

        let jon_jones = Subject {
            name: "Jon Jones".to_string(),
            link: "http://www.jonjones.com".to_string(),
        };
        let subjects = vec![jon_jones];
        Ok(subjects)
    })
    .from_err()
error[E0277]: the trait bound `hyper::Body: hyper::rt::Future` is not satisfied
  --> src/scrapper.rs:24:14
   |
24 |             .and_then(|res| {
   |              ^^^^^^^^ the trait `hyper::rt::Future` is not implemented for `hyper::Body`
   |
   = note: required because of the requirements on the impl of `futures::future::IntoFuture` for `hyper::Body`

error[E0599]: no method named `from_err` found for type `futures::future::and_then::AndThen<hyper::client::ResponseFuture, hyper::Body, [closure@src/scrapper.rs:24:23: 28:14]>` in the current scope
  --> src/scrapper.rs:29:14
   |
29 |             .from_err::<WebScrapeError>()
   |              ^^^^^^^^
   |
   = note: the method `from_err` exists but the following trait bounds were not satisfied:
           `&mut futures::future::and_then::AndThen<hyper::client::ResponseFuture, hyper::Body, [closure@src/scrapper.rs:24:23: 28:14]> : hyper::rt::Future`
           `&mut futures::future::and_then::AndThen<hyper::client::ResponseFuture, hyper::Body, [closure@src/scrapper.rs:24:23: 28:14]> : hyper::rt::Stream`
           `futures::future::and_then::AndThen<hyper::client::ResponseFuture, hyper::Body, [closure@src/scrapper.rs:24:23: 28:14]> : hyper::rt::Future`

Why doesn't this compile?


回答1:


and_then must return a future, or something that can be converted into a future via the IntoFuture trait. You're returning res.into_body(), which is not a future - it's actually a stream.

To get this to work, you'll need to convert that stream into a future which represents the body having been fully read. There's several options available to you here:

  • res.into_body().concat2(), which concatenates all of the read bytes into a single buffer
  • res.into_body().collect(), which collects all of the chunks of bytes into a Vec
  • res.into_body().into_future(), which will resolve to a tuple containing the first item from the stream, and the remainder of the stream itself (i.e. (T, Stream<Item = T>))

The fact that there's multiple different (and equally valid) ways to represent a Stream as a Future is likely why the former doesn't have a blanket implementation of IntoFuture.

An example of using concat2 is shown in the Hyper docs.



来源:https://stackoverflow.com/questions/55237128/cant-read-a-simple-payload-making-http-request-via-hyperclientclient-the-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!