Handling streaming data

Estimated reading time: 1 minute

Most of the response types are complete in memory. But you can also fetch streaming data as well.

Scoped streaming

There are multiple ways of doing streaming. The safest way is using HttpStatement with scoped execute block:

client.get<HttpStatement>.execute { response: HttpResponse ->
    // Response is not downloaded here.
    val channel = response.receive<ByteReadChannel>()
}

After execute block is finished, network resources is released.

You can also point different type for execute method:

client.get<HttpStatement>.execute<ByteReadChannel> { channel: ByteReadChannel ->
    // ...
}