title: Forwarding/redirecting a POST request from one REST API to another
body: <p>I'm looking to implement a way of directly forwarding a POST request from one SpringBoot application to another. Forwarding GET requests has been easy, and the code below shows how I get a 405 error when I lazily try to re-use the same redirection pathway.</p>
  
  <p>I have looked at existing examples on StackOverflow (most notably <a href="https://stackoverflow.com/questions/38829595/forward-json-post-request-from-one-rest-api-to-another">here</a>) but I can't even get that solution to compile. Can anyone suggest amendments to the code below to allow me to just redirect the entire POST request through?</p>
  
  <pre><code>@RestController
  public class Routing {
  
      @Autowired
      private RoutingDelegate routingDelegate;
  
      @RequestMapping(value="/**", method=RequestMethod.GET, produces=MediaType.TEXT_PLAIN_VALUE)
      public ResponseEntity&lt;String&gt; catchAll(HttpServletRequest request, HttpServletResponse response){
          return routingDelegate.redirect(request, response);
      }
  
      @RequestMapping(value="/**", method=RequestMethod.POST, produces=MediaType.TEXT_PLAIN_VALUE)
      public ResponseEntity&lt;String&gt; catchAllPost(HttpServletRequest request, HttpServletResponse response){
          /**
           * This returns a 405 error, which is understandable since I'm redirecting a POST request to a GET endpoint
           */
          return routingDelegate.redirect(request, response);
      }
  }
  </code></pre>
  
  <p>&nbsp;</p>
  
  <pre><code>@Service
  public class RoutingDelegate {
  
      private final String baseServerUrl = "http://localhost:8080";
  
      public ResponseEntity&lt;String&gt; redirect(HttpServletRequest request, HttpServletResponse response){
          try {
              String queryString = request.getQueryString();
              String redirectUrl = baseServerUrl + request.getRequestURI() +
                      (queryString != null ? "?" + queryString : "");
              response.sendRedirect(redirectUrl);
          }
          catch (IOException e) {
              return new ResponseEntity&lt;String&gt;("REDIRECT ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
          }
          return new ResponseEntity&lt;String&gt;("", HttpStatus.OK);
      }
  }
  </code></pre>