2

我正在尝试从 Rust subreddit 获取帖子并将响应返回到我的前端。

这是我的App

App::new()
    .data(Client::default())
    .wrap(middleware::Logger::default())
    .service(
        web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts))
    )

处理函数:

fn get_rust_posts(req: HttpRequest, client: web::Data<Client>) -> impl Future<Item = HttpResponse, Error = Error> {
  client.get("http://www.reddit.com/r/rust.json") // create request builder
    .header("User-Agent", "Actix-web")
    .send() // send http request
    .map_err(Error::from)
    .and_then(|resp| {
      // return resp
    })
}

依赖项:

[dependencies]
actix-web = "1.0.7"
futures = "0.1.29"
4

1 回答 1

1
fn get_rust_posts(req: HttpRequest, client: web::Data<Client>) -> impl Future<Item = HttpResponse, Error = Error> {
  client.get("http://www.reddit.com/r/rust.json") // create request builder
    .header("User-Agent", "Actix-web")
    .send() // send http request
    .map_err(Error::from)
    .and_then(|resp| Ok::<HttpResponse, Error>(HttpResponse::Ok().streaming(resp)))
}

它适用于 Ubuntu Linux,但目前不适用于 Windows

于 2019-10-01T14:28:33.070 回答