I'd like to have an Actix Web handler which responds to a POST request by printing the POST body to the console and constructing an HTTP response that contains the current URL from the request object.
When reading the request's POST body, futures seem to need to be involved. The closest I've gotten so far is:
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()
}
This won't compile because the future outlives the handler, so my attempts to read req.connection_info()
are illegal. The compiler error suggests I use the move
keyword into the closure definition, i.e. .and_then(move |bytes: Bytes| {
. This also won't compile because req
gets moved on the req.body()
call and is then captured after the move in the references constructing url
.
What is a reasonable way of constructing a scope in which I have access to data attached to the request object (e.g. the connection_info
) at the same time as access to the POST body?