rust - 将数据附加到 Actix-web 中的代理响应

标签 rust future rust-actix

我想将用户的请求数据附加到 actix-web 中的过氧化响应,但是会发生类型不匹配解决错误。

我认为这是关于 rust futures 但我不知道这个问题以及如何解决它。

示例代码:

use actix_web::*;
use futures::future::ok;
use futures::Future;

fn show_request(
    request: &actix_web::HttpRequest,
) -> Box<Future<Item = HttpResponse, Error = Error>> {
    let mut result: Vec<u8> = Vec::new();
    Box::new(request.body().map_err(|e| e.into()).map(move |f| {
        result.extend(f); 
        client::ClientRequest::get("http://example.com/")
            .finish().unwrap()
            .send()
            .map_err(Error::from)
            .and_then(
                |resp| resp.body() 
                    .from_err()  
                    .and_then(|body| { 
                        result.extend(body); // append request body to response proxied site
                        Ok(HttpResponse::Ok().body(result))
                    }))


    }))
}

pub fn index(scope: actix_web::Scope<()>) -> actix_web::Scope<()> {
    scope.handler("", |req: &actix_web::HttpRequest| {
        show_request(req)
    })
}

fn main() {
    actix_web::server::new(|| {
        vec![
            actix_web::App::new()
                .scope("", index)
                .boxed(),

        ]
    }).bind("127.0.0.1:8000")
        .expect("Can not bind to port 8000")
        .run();
}

cargo .toml

[package]
name = "temp"
version = "0.1.0"
authors = ["John"]
edition = "2018"

[dependencies]
actix-web = "0.7"
futures = "0.1"

显示错误:

error[E0271]: type mismatch resolving `<[closure@src/main.rs:9:55: 24:6 result:_] as std::ops::FnOnce<(bytes::bytes::Bytes,)>>::Output == actix_web::httpresponse::HttpResponse`
  --> src/main.rs:9:5
   |
9  | /     Box::new(request.body().map_err(|e| e.into()).map(move |f| {
10 | |         result.extend(f);
11 | |         client::ClientRequest::get("http://example.com/")
12 | |             .finish().unwrap()
...  |
23 | |
24 | |     }))
   | |_______^ expected struct `futures::future::and_then::AndThen`, found struct `actix_web::httpresponse::HttpResponse`
   |
   = note: expected type `futures::future::and_then::AndThen<futures::future::map_err::MapErr<actix_web::client::pipeline::SendRequest, fn(actix_web::client::pipeline::SendRequestError) -> actix_web::error::Error {<actix_web::error::Error as std::convert::From<actix_web::client::pipeline::SendRequestError>>::from}>, futures::future::and_then::AndThen<futures::future::from_err::FromErr<actix_web::httpmessage::MessageBody<actix_web::client::response::ClientResponse>, actix_web::error::Error>, std::result::Result<actix_web::httpresponse::HttpResponse, actix_web::error::Error>, [closure@src/main.rs:18:19: 21:10 result:_]>, [closure@src/main.rs:16:8: 21:11 result:_]>`
              found type `actix_web::httpresponse::HttpResponse`
   = note: required because of the requirements on the impl of `futures::future::Future` for `futures::future::map::Map<futures::future::map_err::MapErr<actix_web::httpmessage::MessageBody<actix_web::httprequest::HttpRequest>, [closure@src/main.rs:9:37: 9:49]>, [closure@src/main.rs:9:55: 24:6 result:_]>`
   = note: required for the cast to the object type `dyn futures::future::Future<Error=actix_web::error::Error, Item=actix_web::httpresponse::HttpResponse>

最佳答案

您可以使用 future 组合器解决您的问题。

例如:

Box::new(request.body().map_err(|e| e.into()).and_then(|mut x|{
    client::ClientRequest::get("http://example.com/")
        .finish()
        .unwrap()
        .send()
        .map_err(Error::from)
        .and_then(|resp| {
            resp.body()
                .from_err()
                .and_then(|body| {
                    x.extend(body);
                    Ok(HttpResponse::Ok().body(x))
                })
        })
}))

关于rust - 将数据附加到 Actix-web 中的代理响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54306821/

相关文章:

rust - 使用类型作为不同的名称和类型别名有什么区别?

rust - 如何将 panic=abort 与外部依赖项一起使用?

scala - 为什么全局 ExecutionContext 不是 future block 中的默认参数?

java - 在 Callable 中处理 Thread.interrupted() 的正确方法?

rust - 如何在 actix-web 中执行异步函数?

rust - 如何为 actix-web HttpResponse 创建流以逐 block 发送文件?

regex - 迭代正则表达式捕获的生命周期问题

error-handling - 如何使用问号运算符来处理 Tokio future 中的错误?

c++ - 如何对涉及返回非 void 类型的用户定义类的成员函数的线程使用引用包装 (c++)

websocket - 通过对 websockets 的多个引用向客户端发送消息