body

inline suspend fun <T> body(): T

Executes the HTTP statement and processes the response through HttpClient.responsePipeline to retrieve an instance of the specified type T.

If T represents a streaming type (such as ByteReadChannel), it is the caller's responsibility to properly manage the resource, ensuring it is closed when no longer needed.

Return

The response body transformed to the specified type T.


inline suspend fun <T, R> body(crossinline block: suspend (response: T) -> R): R

Executes the HTTP statement and processes the response of type T through the provided block.

This function is particularly useful for handling streaming responses, allowing you to process data on-the-fly while the network connection remains open. The block receives the streamed response and can be used to perform operations on the data as it arrives.

Once block completes, the resources associated with the response are automatically cleaned up, freeing any network or memory resources held by the response.

Usage Example

client.request {
url("https://ktor.io")
}.body<ByteReadChannel> { channel ->
// Process streaming data here
}
// Resources are released automatically after block completes

Return

The result of block applied to the streaming response.

Parameters

block

A suspend function that handles the streamed response of type T.