title: Undertow - How to Setup a Reverse Proxy to Monitor HTTP/HTTPS Traffic to my REST API
body: <p>I would like to basically sniff HTTP/HTTPS requests and responses from my computer to a remote URL (basically a REST API). I understand there are plenty of tools that will help me get what I want, but I have to kind of write a utility to do this in Java (as this's going to be used by some applications we are developing).</p>
  
  <p>What I want to do is set up a proxy (or a reverse proxy - my understanding of the terms is limited here), through which I route all my requests to real APIs. This proxy must Capture the Request body (in case of a POST or PUT) and forward the request to the real API, capture its response and send it back to the client.</p>
  
  <p>I came across <strong>Undertow</strong>, using which I was able to create and run a Reverse Proxy using its <code>LoadBalancingProxyClient</code> proxy client. But I have a few questions here.</p>
  
  <p>Assume my API is <code>http://myrealapplication.com/rest/operation1</code>, and I want to POST a JSON to this API.</p>
  
  <p>I have created a proxy server as follows:</p>
  
  <pre><code>        LoadBalancingProxyClient loadBalancer = new LoadBalancingProxyClient();
          loadBalancer.addHost(new URI("http://myrealapplication.com"));
          loadBalancer.setConnectionsPerThread(20);
  
          Undertow reverseProxy = Undertow.builder()
                  .addHttpListener(8990, "localhost")
                  .setIoThreads(4)
                  .setHandler(Handlers.requestDump(new ProxyHandler(loadBalancer, 3000, null)))
                  .build();
          reverseProxy.start();
  </code></pre>
  
  <p>So once I've started my Reverse Proxy server, there are two things:</p>
  
  <ol>
  <li>My reverse proxy has practically become sort of an alias for my real API, but how do I capture the request and response in this layer?</li>
  <li>With the above setup, in order for my request to pass through this proxy, I have to change my URL to <code>http://localhost:8990/rest/operation1</code>, which may not be acceptable, as what we are expecting to do is to set up <code>localhost:8990</code> as a <strong>Proxy</strong>, and keep my URL as <code>http://myrealapplication.com/rest/operation1</code>, so that it looks to the end client as the response is coming from the actual API.</li>
  </ol>
  
  <p>Can I use undertow to achieve this? Or are there any other tools which expose a Java API which I can use in my programs?</p>
  
  <p>Please help!</p>
  
  <p>Thanks,
  Sriram</p>