I make a request to the https URL with the postman, I get 400 response and data without any certification config. While I make the same request with restTemplate I get 404 not found with the same url. postman body :
{
"Request": {
"usernamenull": "",
"passwordnull": ""
},
"username": "iceicebaby",
"password": "12345"
}
and Java code:
HttpHeaders headers= new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization","Basic ***");
headers.add("Accept", "*/*");
RequestBodyModel request = new RequestBodyModel();
request.setRequest(new UserRequest("",""));
request.setUsername(userName);
request.setPassword(password);
HttpEntity<RequestBodyModel> requestEntity = new HttpEntity<>(request,headers);
String response = restTemplate.postForObject(url,requestEntity,String.class);
any help appreciated, thanks.
EDIT: I changed my code to :
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST,requestEntity,String.class);
and I get 302 redirect error(Location: same url, server: 'ACB') while postman works perfectly.
The difference between postman and restTemplate is because of their ways to handle redirect.
Postman automatically redirect your request. As you can see in this screenshot, I think you're postman's automatic redirect option will be turned on too.
But, restTemplate doesn't automatically redirect your requests.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
.
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy()) // Enables automatic redirect handling for all HTTP methods
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
if (responseEntity.getStatusCode() == HttpStatus.FOUND) { // 302 redirect
String redirectUrl = responseEntity.getHeaders().getLocation().toString();
// get the redirect Url from header
ResponseEntity<String> redirectedResponse = restTemplate.exchange(redirectUrl, HttpMethod.GET, new HttpEntity<>(headers), String.class);
}