diff --git a/web-clients/src/main/java/org/bahmni/webclients/HttpClient.java b/web-clients/src/main/java/org/bahmni/webclients/HttpClient.java index bd6fb74..110aaf8 100644 --- a/web-clients/src/main/java/org/bahmni/webclients/HttpClient.java +++ b/web-clients/src/main/java/org/bahmni/webclients/HttpClient.java @@ -99,9 +99,16 @@ public R post(String url, T payload, Class returnType) throws IOExcept private void checkSanityOfResponse(HttpResponse httpResponse) { StatusLine statusLine = httpResponse.getStatusLine(); int statusCode = statusLine.getStatusCode(); - if (statusCode < 200 || statusCode >= 300) throw new WebClientsException("Bad response code of " + statusCode); - HttpEntity entity = httpResponse.getEntity(); + if (statusCode < 200 || statusCode >= 300) { + try { + EntityUtils.consume(entity); + } catch (IOException e) { + throw new WebClientsException("Unable to read response entity", e); + } + throw new WebClientsException("Bad response code of " + statusCode); + } + if (entity == null) throw new WebClientsException("Cannot read response"); }