Skip to content

Commit 02595ba

Browse files
kwoykeKrzysztof Woyke
and
Krzysztof Woyke
authored
BAEL-4970: Close the original response body (eugenp#10782)
Co-authored-by: Krzysztof Woyke <[email protected]>
1 parent fd71e2c commit 02595ba

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

libraries-http-2/src/main/java/com/baeldung/okhttp/interceptors/ErrorResponseInterceptor.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ public Response intercept(Chain chain) throws IOException {
2121
Gson gson = new Gson();
2222
String body = gson.toJson(new ErrorMessage(response.code(), "The response from the server was not OK"));
2323
ResponseBody responseBody = ResponseBody.create(body, APPLICATION_JSON);
24-
24+
25+
ResponseBody originalBody = response.body();
26+
if (originalBody != null) {
27+
originalBody.close();
28+
}
29+
2530
return response.newBuilder()
2631
.body(responseBody)
2732
.build();

0 commit comments

Comments
 (0)