Rust 将数据附加到Actix web中的代理响应
我想将用户的请求数据附加到Rust 将数据附加到Actix web中的代理响应,rust,future,rust-actix,Rust,Future,Rust Actix,我想将用户的请求数据附加到actixweb中的过氧化响应中,但是类型不匹配解决将发生错误 我认为这是关于生锈的未来,但我不知道这个问题,我如何才能解决它 示例代码: use actix_web::*; use futures::future::ok; use futures::Future; fn show_request( request: &actix_web::HttpRequest, ) -> Box<Future<Item = HttpRespons
actixweb
中的过氧化响应中,但是类型不匹配解决将发生错误
我认为这是关于生锈的未来,但我不知道这个问题,我如何才能解决它
示例代码:
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();
}
显示错误:
error[E0271]:类型不匹配解决`::Output==actix\u web::httpresponse::httpresponse`
-->src/main.rs:9:5
|
9 |/Box::new(request.body().map|u err(| e | e.into()).map(move | f |{
10 | |结果扩展(f);
11 | |客户端::ClientRequest::获取(“http://example.com/")
12 | |.finish().unwrap()
... |
23 | |
24 | | }))
||uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu`
|
=注意:预期类型为'futures::future::and_then::然后,futures::future::and_then::然后[closure@src/main.rs:16:8:21:11结果:(]>`
找到类型“actix_web::httpresponse::httpresponse”`
=注意:由于'futures::future::future'的impl对'futures::future::map::map'的要求,因此需要`
=注意:强制转换为对象类型“dyn futures::future::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))
})
})
}))
什么是组合子?
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))
})
})
}))