Can one post binary data (not multipart) from a `j...
# ktor
n
Can one post binary data (not multipart) from a
java.io.File
like this?
Copy code
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.
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
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
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
Maybe the problem is that in case of the
readChannel
the
Transfer-Encoding: chunked
header is sent.
n
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!