title: How to pipe request data from spring MVC controller to Spring RestTemplate
body: <p>I have a Spring MVC application which takes requests from UI (multiform and json) and it has to post this data to another micro service using Spring RestTemplate. Copying request as string to RestTemplate works fine incase of json content type but doesnt seems to be working incase of multipart.</p>
  
  <p>Here is my sample code</p>
  
  <p>Spring MVC controller:</p>
  
  <pre><code>@Controller
  public class MvcController {
  
    @RequestMapping(value = "/api/microservice",  method = RequestMethod.POST)
    public ResponseEntity&lt;?&gt; callMicroservice(HttpServletRequest request) throws Exception {
        RestTemplate rest = new RestTemplate();
        String payload = IOUtils.toString(request.getReader());
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", request.getHeader("Content-Type"));
        HttpEntity&lt;String&gt; requestEntity = new HttpEntity&lt;String&gt;(payload, headers);
        return rest.exchange("https://remote.micrservice.com/api/backendservice", HttpMethod.POST, requestEntity, String.class);    
    }
  }
  </code></pre>
  
  <p>And here how backend microservice looks like</p>
  
  <pre><code>@Controller
  public class RestController {
  
    @RequestMapping(value = "/api/backendservice",  method = RequestMethod.POST)
    public @ResponseBody Object createService(@RequestParam(value = "jsondata") String jsondata,
          @RequestParam(value = "email") String email,@RequestParam(value = "xsltFile", required = false) MultipartFile xsltFile,
          HttpServletRequest request) {
        // process jsondata
        // process xsltFile
        // send response
    }
  }
  </code></pre>
  
  <p>If you look at MvcController, i am sending payload as string </p>
  
  <pre><code>String payload = IOUtils.toString(request.getReader());
  </code></pre>
  
  <p>instead, how can I send request data as it is to RestTemplate request so that it works for both string and multipart. If you look at MvcController signature, I do not know what details user would be sending at the sometime I do not know what would be micro service signature. I just need to pipe the data between MvcController and RestTemplate request.</p>