title: Java - Resttemplate - POST with multipart/form-data
body: <p>I have to make a POST request to the WALMART API in order to upload my product onto their platform. I have an xml file containing the request to be sent.
  My request works in ARC/Postman (can see below example of how the working version is), problem is I cannot make it work in Java using Resttemplate, I simply cannot set the body request (the actual file) properly.</p>
  
  <p>Can you help with 'translating/converting' the request from the below image into Java using Resttemplate?</p>
  
  <p>I have this code so far for setting the above headers(hardcoded some values for security reasons):</p>
  
  <pre><code>    headers = new HttpHeaders();
      headers.set("WM_SVC.NAME", "Walmart Marketplace");
      headers.set("WM_QOS.CORRELATION_ID", "123456abcdef");
      headers.set("WM_SEC.TIMESTAMP", timestamp);
      headers.set("WM_SEC.AUTH_SIGNATURE", signatureString);
      headers.set("WM_CONSUMER.ID", "my-consumer-id-cut-for-security-reasons");
      headers.set("WM_CONSUMER.CHANNEL.TYPE", "my-channel-type");
      headers.setContentType(MediaType.MULTIPART_FORM_DATA);
      headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
      headers.set("Host", "https://marketplace.walmartapis.com");
  </code></pre>
  
  <p>and the actual request: </p>
  
  <pre><code>File file = new File("C:\\walmartfeed.xml");
  DiskFileItem fileItem = new DiskFileItem("file", "text/xml", false, file.getName(), (int) file.length(), file.getParentFile());
  fileItem.getOutputStream();
  MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
  
  MultiValueMap&lt;String, Object&gt; parts =
          new LinkedMultiValueMap&lt;String, Object&gt;();
  parts.add("file", new ByteArrayResource(multipartFile.getBytes()));
  
  HttpEntity&lt;MultiValueMap&lt;String, Object&gt;&gt; request = new HttpEntity&lt;&gt;(parts, headers);
  restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
  restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
  restTemplate.getMessageConverters().add(new MultipartAwareFormHttpMessageConverter());
  
  ResponseEntity&lt;FeedAcknowledgement&gt; response = restTemplate.exchange("https://marketplace.walmartapis.com/v3/feeds?feedType=item", org.springframework.http.HttpMethod.POST, request, FeedAcknowledgement.class);
  </code></pre>
  
  <p>Thank you</p>
  
  <p><a href="https://i.stack.imgur.com/hNuYJ.png" rel="nofollow noreferrer">this is the link for the image</a></p>