title: How To send a Post request with RequetTemplate in Spring
body: <p>I'm trying to send a post request with spring RequestTemplate but I always get 401 error;</p>
  
  <p>using curl</p>
  
  <pre><code>C:\Users\Latitude E 5410&gt;curl -X POST http://localhost:8080/demo.rest.springsecu
  rity.oauth2.0.authentication/oauth/token -H "Accept: application/json" -d "usern
  ame=user1&amp;password=user1&amp;client_id=client1&amp;client_secret=client1&amp;grant_type=pass
  word&amp;scope=read,write"
  {"access_token":"abe9d772-cb29-4bd7-b41a-437e50c10652","token_type":"bearer","re
  fresh_token":"bc24f370-fcd8-4ae0-b724-a4241c746b29","expires_in":299844,"scope":
  "read,write"}curl: (6) Could not resolve host: application
  </code></pre>
  
  <p>java code used:</p>
  
  <pre><code>HttpEntity&lt;String&gt; request = new HttpEntity&lt;String&gt;(getHeadersWithClientCredentials());
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON); //restTemplate.postForObject
          //restTemplate.
           MultiValueMap&lt;String, String&gt; mvm = new LinkedMultiValueMap&lt;String, String&gt;();
              mvm.add("client_id", "client1");
              mvm.add("client_secret", "client1");
              mvm.add("grant_type", "password");
              mvm.add("scope", "read,write,trust");
              mvm.add("username", "user1");
              mvm.add("password", "user1");
              HttpHeaders requestHeaders = new HttpHeaders();
              requestHeaders.setContentType(MediaType.APPLICATION_JSON);
              HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; requestEntity = new HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt;(mvm, requestHeaders);
              ResponseEntity&lt;Object&gt; response = restTemplate.exchange("http://localhost:8080/demo.rest.springsecurity.oauth2.0.authentication/oauth/token?", HttpMethod.POST, requestEntity, Object.class);
  </code></pre>
  
  <p>error I get</p>
  
  <pre><code>Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Non-Autorisé
      at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
      at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
      at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)
      at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:557)
      at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:475)
      at com.geofleet.calculation.TestClient.main(TestClient.java:44)
  </code></pre>