title: Mojolicious - acting as an http proxy
body: <p>I'm trying to implement a simple http proxy for certain requests for a simple web service using JSON as the payload.</p>
  
  <p>I see some information on this, but no complete examples in the documentation. I've tried some code from blog posts that I found in google search results, but nothing that I could get working. Here is where I am.</p>
  
  <p>The request is passed to a handler object, with a handle method like so</p>
  
  <pre><code>sub handle
  {
      my $self = shift;
      my $app = shift;
  
      my $tx = $app-&gt;tx;
      my $req = $app-&gt;req-&gt;clone;
      $log-&gt;info("upstream host is " . $self-&gt;{upstream_host});
      $log-&gt;info("upstream port is " . $self-&gt;{upstream_port});
      $req-&gt;url-&gt;scheme("http")
              -&gt;host($self-&gt;{upstream_host})
              -&gt;port($self-&gt;{upstream_port});
  
      $log-&gt;info("req: " . $req-&gt;to_string);
      $log-&gt;info("req host:port " . $req-&gt;url-&gt;host . ":" . $req-&gt;url-&gt;port);
  
      my $ua = Mojo::UserAgent-&gt;new;
      my $tx = Mojo::Transaction::HTTP-&gt;new(req =&gt; $req);
  
      $log-&gt;info("response: " . $tx-&gt;res-&gt;to_string());
  
      $app-&gt;render(data =&gt; $tx-&gt;res-&gt;body);
  }
  </code></pre>
  
  <p>Everything in the logs looks correct, but the response object is a 404 error, and no actual traffic reaches my other service listening on localhost:3334.</p>
  
  <p>2013-07-02 12:28:26.929793500 [Tue Jul  2 12:28:26 2013] [info] req host:port 127.0.0.1:3334
  2013-07-02 12:28:26.930225500 [Tue Jul  2 12:28:26 2013] [info] response: HTTP/1.1 404 Not Found</p>
  
  <p>I tried the non-blocking version of this but again, no request was actually sent, so I tried to simplify.</p>
  
  <p>Help appreciated, I'm not sure where I've gone wrong here. I'm just trying to duplicate the request, pass it to another service, and then return its response to the original client.</p>
  
  <p>Thanks,
  Mike</p>