HttpResponseBody
Represents the body of an HTTP response.
The body is backed by either a buffer (for in-memory responses), a file (for cached responses), or a socket (for streaming network responses). It's crucial to close the body after consuming it to prevent resource leaks, as both HttpResponse and HttpResponseBody implement AutoCloseable.
Here are the ways to close the body:
HttpResponse.close() - This delegates the close() call to the body.
HttpResponse.body().close() - This explicitly closes the body.
HttpResponse.body().byteStream().close() - This ensures that the backing InputStream is closed.
HttpResponse.body().consumeBytes() - This reads everything from the InputStream and then closes it.
Content copied to clipboard
For synchronous calls, the most convenient way to ensure that a response body is closed is to use a Kotlin's use
block. This automatically closes the resource at the end of the block.
Failure to close a response body may result in resource leaks and should be done in all cases.
Important: This is a Public Preview API. It may be changed or removed at any time.