title: Spring RestTemplate credential/Authorization in header getting 401-unauthorized, where in postman it is working fine
body: <p>using postman, for a GET request with header values for user name and password 
  and successfully hitting a rest service and getting response 200.</p>
  
  <p><a href="https://i.stack.imgur.com/bxIkc.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bxIkc.jpg" alt="enter image description here"></a></p>
  
  <p>But when trying to access same request by java code using spring <code>RestTemplate</code> getting <code>401-unauthorized</code> issue.</p>
  
  <p>this is the code </p>
  
  <pre><code>      final String uri = "http://&lt;host&gt;:&lt;port&gt;/services/arecord";
  
          HttpHeaders headers = new HttpHeaders();
          headers.setContentType(MediaType.APPLICATION_JSON);
          MultiValueMap&lt;String, String&gt; map = new LinkedMultiValueMap&lt;&gt;();
          map.add("username", "admin");
          String password = "admin";
  
          map.add("password", password);
          HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; request = new HttpEntity&lt;&gt;(map, headers);
  
          RestTemplate restTemplate = new RestTemplate();
  
          try {
              ResponseEntity&lt;String&gt; response = restTemplate.exchange(uri, HttpMethod.GET,
                      new HttpEntity(createHeaders("admin", "admin")), String.class);
              String body = response.getBody();
  
          } catch (HttpClientErrorException e) {
  
              logger.info("****** ERROR *********** " + e.getMostSpecificCause());
              return true;
          }
  </code></pre>