Nikky
03/13/2024, 11:21 AMexecute
block with this code
val channel: ByteReadChannel = response.body()
flow {
logger.warn { "closed for read: " + channel.isClosedForRead }
while (!channel.isClosedForRead) {
val line = channel.readUTF8Line()
if(line != null) {
emit(line)
}
}
}
but it seems that channel.isClosedForRead
is always true
maybe because there is no content-length header ? i don't really know
curl is able to stream the data just fineAleksei Tirman [JB]
03/13/2024, 11:47 AMNikky
03/13/2024, 1:06 PMresponse.body()
or response.bodyAsChannel()
Nikky
03/13/2024, 1:09 PMresponse.contentLength()
returns `null`as there is a unknown amount of data (ndjjson stream)
is there a header to indicate that?Nikky
03/13/2024, 1:15 PMAleksei Tirman [JB]
03/13/2024, 1:49 PMAleksei Tirman [JB]
03/13/2024, 1:49 PMval client = HttpClient(CIO) {
}
client.prepareGet("<http://localhost:8082/>").execute { response ->
val channel: ByteReadChannel = response.body()
while (!channel.isClosedForRead) {
val line = channel.readUTF8Line()
if (line != null) {
println(line)
}
}
}
Server:
embeddedServer(Netty, port = 8082) {
routing {
get {
call.respondBytesWriter {
repeat(10) {
writeStringUtf8("hello $it\n")
flush()
delay(500)
}
}
}
}
}.start(true)
Aleksei Tirman [JB]
03/13/2024, 1:50 PMContent-Length
header.Nikky
03/13/2024, 1:54 PMNikky
03/13/2024, 2:41 PMAleksei Tirman [JB]
03/13/2024, 3:11 PMval client = HttpClient(CIO) {
}
val f = flow {
client.prepareGet("<http://localhost:8082/>").execute { response ->
val channel: ByteReadChannel = response.body()
while (!channel.isClosedForRead) {
val line = channel.readUTF8Line()
if (line != null) {
emit(line)
}
}
}
}
f.collect {
println(it)
}
Nikky
03/13/2024, 3:53 PM