title: 401 Malformed HTTP basic Authorization header: Error on executing api through RestTemplate
body: <p>I am trying to execute an api. This API is triggering a build on Jenkins. I am encoding my username and password and then setting it to headers. But it is giving me below error </p>
  
  <blockquote>
    <p>org.springframework.web.client.HttpClientErrorException: 401 Malformed
    HTTP basic Authorization header.</p>
  </blockquote>
  
  <p>The api is executing fine with this curl:</p>
  
  <pre><code>curl -kX POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'https://XYZ' --user "username:password"
  </code></pre>
  
  <p>Jave programme:</p>
  
  <pre><code>TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -&gt; true;
  SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
  
  SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
  
  CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
  
  HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
  
  requestFactory.setHttpClient(httpClient);
  String encodeBytes = Base64.getEncoder().encodeToString(("username" + "password").getBytes());
  ManagementController.restTemplate = new RestTemplate(requestFactory);
  HttpHeaders headers = new HttpHeaders();
  headers.set("Authorization", "Basic " + encodeBytes);
  headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
  HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(headers);
  ResponseEntity&lt;String&gt; response = restTemplate.exchange(deployURL,HttpMethod.POST, entity, String.class);
  String message=response.getBody();
  return modelAndView;
  </code></pre>
  
  <p>Thanks in advance!</p>