title: How do I access HttpRequest data inside a Future in Actix-web?
body: <p>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.</p>
  
  <p>When reading the request's POST body, futures seem to need to be involved. The closest I've gotten so far is:</p>
  
  <pre><code>fn handler(req: HttpRequest) -&gt; FutureResponse&lt;HttpResponse&gt; {
      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()
  }
  </code></pre>
  
  <p>This won't compile because the future outlives the handler, so my attempts to read <code>req.connection_info()</code> are illegal. The compiler error suggests I use the <code>move</code> keyword into the closure definition, i.e. <code>.and_then(move |bytes: Bytes| {</code>. This also won't compile because <code>req</code> gets moved on the <code>req.body()</code> call and is then captured after the move in the references constructing <code>url</code>.</p>
  
  <p>What is a reasonable way of constructing a scope in which I have access to data attached to the request object (e.g. the <code>connection_info</code>) at the same time as access to the POST body?</p>