我想要一个 Actix Web 处理程序,它通过将 POST 正文打印到控制台并构建包含来自请求对象的当前 URL 的 HTTP 响应来响应 POST 请求。
在读取请求的 POST 正文时,似乎需要涉及 future 。到目前为止我得到的最接近的是:
fn handler(req: HttpRequest) -> FutureResponse<HttpResponse> {
req.body()
.from_err()
.and_then(|bytes: Bytes| {
println!("Body: {:?}", bytes);
let url = format!("{scheme}://{host}",
scheme = req.connection_info().scheme(),
host = req.connection_info().host());
Ok(HttpResponse::Ok().body(url).into())
}).responder()
}
这不会编译,因为 future 比处理程序还长,所以我尝试读取 req.connection_info()
是非法的。编译器错误建议我在闭包定义中使用 move
关键字,即 .and_then(move |bytes: Bytes| {
。这也不会编译,因为 req
在 req.body()
调用中移动,然后在移动后在构造 url
的引用中被捕获。
在访问 POST 正文的同时,构建一个范围的合理方法是什么?我可以在其中访问附加到请求对象的数据(例如 connection_info
)?
最佳答案
最简单的解决方案是在未来完全不访问它:
extern crate actix_web; // 0.6.14
extern crate bytes; // 0.4.8
extern crate futures; // 0.1.21
use actix_web::{AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse};
use bytes::Bytes;
use futures::future::Future;
fn handler(req: HttpRequest) -> FutureResponse<HttpResponse> {
let url = format!(
"{scheme}://{host}",
scheme = req.connection_info().scheme(),
host = req.connection_info().host(),
);
req.body()
.from_err()
.and_then(move |bytes: Bytes| {
println!("Body: {:?}", bytes);
Ok(HttpResponse::Ok().body(url).into())
})
.responder()
}
如果这不仅仅是出于演示目的的快速破解,通过连接字符串构建 URL 是一个糟糕的想法,因为它不能正确地转义值。您应该使用可以为您执行此操作的类型。
关于rust - 如何访问 Actix-web 中 Future 中的 HttpRequest 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51225281/