diff --git a/src/main/java/com/box/sdk/BoxAPIResponse.java b/src/main/java/com/box/sdk/BoxAPIResponse.java index 6de82fe6c..48d82eb1f 100644 --- a/src/main/java/com/box/sdk/BoxAPIResponse.java +++ b/src/main/java/com/box/sdk/BoxAPIResponse.java @@ -149,11 +149,15 @@ static BoxAPIResponse toBoxResponse(Response response) { } ResponseBody responseBody = response.body(); if (responseBody.contentLength() == 0 || responseBody.contentType() == null) { - return new BoxAPIResponse(response.code(), - response.request().method(), - response.request().url().toString(), - response.headers().toMultimap() - ); + try { + return new BoxAPIResponse(response.code(), + response.request().method(), + response.request().url().toString(), + response.headers().toMultimap() + ); + } finally { + responseBody.close(); + } } if (responseBody != null && responseBody.contentType() != null) { if (responseBody.contentType().toString().contains(APPLICATION_JSON)) { @@ -170,6 +174,8 @@ static BoxAPIResponse toBoxResponse(Response response) { throw new BoxAPIException(format("Error parsing JSON:\n%s", bodyAsString), e); } catch (IOException e) { throw new RuntimeException("Error getting response to string", e); + } finally { + responseBody.close(); } } }