val channel = file.readChannel()
return <http://ktor.post|ktor.post>("endpoint") {
setBody(channel)
contentType(ContentType.Application.OctetStream)
}.body()
Server returns 500 and I’m trying to understand if it’s my fault or not. I found
readChannel()
by accident, but it seemed useful in order to avoid loading the whole file in JVM memory as a ByteArray for example.
natario1
08/08/2023, 3:32 PM
Another Channel-related test (as said, I’m not confident with the interface, just playing with it):
Copy code
setBody(ChannelWriterContent(
contentType = contentType,
body = {
val read = file.readChannel()
read.copyTo(this)
}
))
It fails too.
setBody(file.readBytes())
works.
a
Aleksei Tirman [JB]
08/09/2023, 7:02 AM
The following code works on my machine:
Copy code
val channel = File("path/to/file").readChannel()
val client = HttpClient()
val response = <http://client.post|client.post>("<https://httpbin.org/post>") {
setBody(channel)
contentType(ContentType.Application.OctetStream)
}
println(response.bodyAsText())
n
natario1
08/09/2023, 9:35 AM
Thanks Aleksei, I have the same code. If I replace
readChannel()
with
readBytes()
, our server is happy, otherwise it isn’t. So this line creates some difference in the outgoing request, but I can’t figure out what
a
Aleksei Tirman [JB]
08/09/2023, 9:37 AM
Maybe the problem is that in case of the
readChannel
the
Transfer-Encoding: chunked
header is sent.
n
natario1
08/09/2023, 10:18 AM
I see, that’s likely the problem… FWIW I tried passing contentLength to ChannelWriterContent, to avoid the chunked transfer. But backend is still unhappy. Now I don’t think I have more time to spend on this 😄 thank you anyway!