Is it possible to overide HttpRequestHeader Host spring boot

656 Views Asked by At

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?

0

There are 0 best solutions below