I have a Sprint Boot 2 application that uses RestTemplate to call an external web service for a third-party product called ForgeRock. It uses the headers to send parameters (not my idea) instead of the body. I keep getting an Error 400 when trying to POST the request. The body should be empty and I have tried sending null and "{}" with the same results. FYI, my authenticate method returns null for now while debugging.
@PostMapping
public String authenticate(@RequestBody User user) {
RestTemplate restTemplate = new RestTemplate();
try {
HttpHeaders headers = createHttpHeaders(user.getUsername(), user.getPassword());
HttpEntity<String> entity = new HttpEntity<>(null, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(authenticateUrl, HttpMethod.POST, entity, String.class);
System.out.println("RESPONSE: " + responseEntity.getBody());
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
private HttpHeaders createHttpHeaders(String user, String password) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("X-OpenAM-Username", user);
headers.add("X-OpenAM-Password", password);
headers.add("Accept-API-Version", "resource=1.1");
return headers;
}
I was only getting 400 Bad Request, which I took to mean an issue with the JSON. However, I discovered the underlying error was actually from ForgeRock that I wasn't sending an FQDN (I was sending IP). So, the code works as-is if it is sent a FQDN.