Ktor HttpClient Support for Chunked Transfer Encoding
Asked Answered
G

3

6

I'm using the Ktor HttpClient(CIO) to make requests against an HTTP API whose response uses chunked transfer encoding.

Is there a way using the Ktor HttpClient(CIO) to get access to the individual Http Chunks in an HttpResponse, when calling an API that uses chunked transfer encoding?

Gameness answered 28/5, 2020 at 15:6 Comment(0)
H
2

This solution works for me:

fun readByChunks(url: String, client: HttpClient) = flow {
    client.preparePost(url).execute {
        val channel: ByteReadChannel = it.body()
        while (!channel.isClosedForRead) {
            val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
            channel.readAvailable(buffer)
            val trimmed = buffer.dropLastWhile { it == 0.toByte() }.toByteArray()
            emit(String(trimmed))
        }
    }
}

Usage:

 val client = HttpClient(CIO)
 readByChunks("https://example.net/path/to/stream", client).onEach {
    println("$it")
 }.launchIn(YourCoroutineScope)
Huba answered 16/10, 2023 at 10:17 Comment(0)
E
0

I guess better late than never:

httpClient.prepareGet("http://localhost:8080/").execute {
    val channel = it.bodyAsChannel()
    while (!channel.isClosedForRead) {
        val chunk = channel.readUTF8Line() ?: break
        println(chunk)
    }
}
Extraversion answered 21/11, 2022 at 18:59 Comment(0)
B
0

This is what to use to call the ollama API. I hope this helps

fun getOllamaResponse(ollamaRequest: OllamaRequest) = flow {
    restClient.preparePost(url) {
        setBody(ollamaRequest)
        contentType(ContentType.Application.Json)
    }.execute { httpResponse ->
        val channel: ByteReadChannel = httpResponse.body()
        val jsonParser = Json { ignoreUnknownKeys = true }
        while (!channel.isClosedForRead) {
            val line = channel.readUTF8Line()
            if (line != null) {
                emit(jsonParser.decodeFromString<OllamaResponse>(line))
            }
        }
    }
}
Buford answered 25/1 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.