I am using RestClient (not Template!) to send a request to another server. Because of a missing header i get a 400 Error but can not read the body. When i make the request with postman i get a body “header XY missing” i need this Error in my Programm, so i need a way to read/log the body.
public boolean myMethod(String body, String baseUrl, String token) {
return restClient.post()
.uri(baseUrl + "/myEndpoint")
.headers(
httpHeaders -> {
httpHeaders.set("Calling-Application", "IGPR");
httpHeaders.set("Content-Type", "application/json");
httpHeaders.set("Locale", "deu-DEU");
httpHeaders.set("Authorization", "Bearer " + token);
})
.body(body)
.exchange((req, res) -> {
try {
if (res.getStatusCode().is2xxSuccessful()) {
return true;
}
} catch (HttpClientErrorException e) {
e.printStackTrace();
} catch ( IOException re ){ //not working!
System.out.println(res.getBody());
}
return false;
});
}
Here is the Loggin Interceptor which should print out the body but due to unknown reasons the interceptor cant print it
public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
logRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
BufferedClientHttpResponseWrapper responseWrapper = new BufferedClientHttpResponseWrapper(response);
logResponse(responseWrapper);
return responseWrapper;
}
private void logRequest(HttpRequest request, byte[] body) throws IOException {
log.debug("===========================request begin================================================");
log.debug("URI : {}", request.getURI());
log.debug("Method : {}", request.getMethod());
log.debug("Headers : {}", request.getHeaders());
log.debug("Request body: {}", new String(body, StandardCharsets.UTF_8));
log.debug("==========================request end================================================");
}
private void logResponse(ClientHttpResponse response) throws IOException {
log.debug("============================response begin==========================================");
log.debug("Status code : {}", response.getStatusCode());
log.debug("Status text : {}", response.getStatusText());
log.debug("Headers : {}", response.getHeaders());
log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
log.debug("=======================response end=================================================");
}
}
public class BufferedClientHttpResponseWrapper implements ClientHttpResponse {
private final ClientHttpResponse response;
private byte[] body;
public BufferedClientHttpResponseWrapper(ClientHttpResponse response) throws IOException {
this.response = response;
this.body = StreamUtils.copyToByteArray(response.getBody());
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream(body);
}
@Override
public String getStatusText() throws IOException {
return response.getStatusText();
}
@Override
public void close() {
response.close();
}
@Override
public org.springframework.http.HttpHeaders getHeaders() {
return response.getHeaders();
}
@Override
public HttpStatusCode getStatusCode() throws IOException {
return response.getStatusCode();
}
}