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.

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.

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
abstract fun byteStream(): InputStream

Consume the body of the response as a byte stream.

Link copied to clipboard

Consume the body of the response as a byte array.

Link copied to clipboard
abstract fun contentType(): String?

Returns the Content-Type of the response body.

Link copied to clipboard
abstract fun length(): Long

Returns the length (in bytes) of the response body.

Inherited functions

Link copied to clipboard
abstract fun close()