1

I am trying to override the host of my RequestHeader based on some online resources it stated that setting sun.net.http.allowRestrictedHeaders to true will enable the host to be edited as shown below:

 System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
 RestTemplate restTemplate = new RestTemplate();


 HttpHeaders headers = new HttpHeaders();
 headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

 headers.set("Host", "my.dummy.test");

 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
 map.add("appId", transactionInformation.getAppId());

 HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);

 ResponseEntity<String> response = restTemplate.postForEntity("https://xys/web/payments",
                request, String.class);

Basically I am trying to set the host to my.dummy.test. However when i check the header after the post is carried out the host is not updated to my.dummy.test.

I am using spring boot starter parent (2.3.0.RELEASE) and java JDK 14. Any advice how i can achieve it please?

Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
user1999453
  • 1,297
  • 4
  • 29
  • 65
  • see if it helps https://stackoverflow.com/questions/43223261/setting-host-header-for-spring-resttemplate-doesnt-work – Ori Marko Jun 11 '20 at 09:56
  • thanks but System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); not working as suggested in the post – user1999453 Jun 11 '20 at 10:13

0 Answers0