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

问题描述 投票:1回答:1

我想将用户的请求数据附加到actix-web中的过氧化物响应中,但是会发生type mismatch resolving错误。

我认为这与生锈的期货有关,但我对这个问题以及如何解决这个问题一无所知。

示例代码:

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>
rust future rust-actix
1个回答
2
投票

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

例如:

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))
                })
        })
}))
© www.soinside.com 2019 - 2024. All rights reserved.