title: Making authenticated POST requests with Spring RestTemplate for Android
body: <p>I have a RESTful API I'm trying to connect with via Android and RestTemplate. All requests to the API are authenticated with HTTP Authentication, through setting the headers of the HttpEntity and then using RestTemplate's <code>exchange()</code> method.</p>
  
  <p>All GET requests work great this way, but I cannot figure out how to accomplish authenticated POST requests. <code>postForObject</code> and <code>postForEntity</code> handle POSTs, but have no easy way to set the Authentication headers.</p>
  
  <p>So for GETs, this works great:</p>
  
  <pre><code>HttpAuthentication httpAuthentication = new HttpBasicAuthentication("username", "password");
  HttpHeaders requestHeaders = new HttpHeaders();
  requestHeaders.setAuthorization(httpAuthentication);
  
  HttpEntity&lt;?&gt; httpEntity = new HttpEntity&lt;Object&gt;(requestHeaders);
  
  MyModel[] models = restTemplate.exchange("/api/url", HttpMethod.GET, httpEntity, MyModel[].class);
  </code></pre>
  
  <p>But POSTs apparently don't work with <code>exchange()</code> as it never sends the customized headers and I don't see how to set the request body using <code>exchange()</code>.</p>
  
  <p>What is the easiest way to make authenticated POST requests from RestTemplate?</p>