Hi,
i encour an issue which is not really understandable. Below is a method which post a body to an external API. In some cases the API returns a 400 Bad Request with more details in its payload (for example if the entity can not be changed or any other logical issue, then the API sends a message in the body back)
When i send the request with the .body(body) iam getting a 400 Bad Request and RestClient thows an IO ResourceAccessException in line .retrieve()
When i remove the body from the request i get a 400 and the message payload as well, but now i can handle the error in the .onStatus() method and no ResourceAccessException is thrown.
I cant find a out, why a 400 thows with a body an exception and when i remove the body, everything works as expected.
May you can check this and document the different behaviour or is that a bug?
public class MyClass(){
public boolean cancelBooking(String myBody, String baseUrl, String token) {
String success = restClient.post()
.uri(baseUrl + "xxx")
.headers(
httpHeaders -> {
httpHeaders.set("Content-Type", "application/json");
httpHeaders.set("Authorization", "Bearer " + token);
})
.body(myBody)
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, (request, response) -> {
String result = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8);
if (result.contains("xxx")) {
throw new WrongStateException("xxx");
}
})
//use response
return false;
.body(String.class);
}
}
Comment From: bclozel
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.