benkuly
09/17/2021, 10:58 AMByteReadChannel
, but then the Content-Length
-header is not set. Setting it manually throws:
Header(s) [Content-Length] are controlled by the engine and cannot be set explicitly
io.ktor.http.UnsafeHeaderException: Header(s) [Content-Length] are controlled by the engine and cannot be set explicitly
Aleksei Tirman [JB]
09/17/2021, 11:41 AMContent-Length
header by overriding contentLength
property of an object of OutgoingContent.ReadChannelContent
class. Here is an example:
val client = HttpClient(Apache)
val channel = ByteChannel()
channel.writeStringUtf8("hello")
channel.close()
val r = <http://client.post|client.post><String>("<https://httpbin.org/post>") {
body = object : OutgoingContent.ReadChannelContent() {
override val contentLength: Long
get() = 5
override fun readFrom(): ByteReadChannel {
return channel
}
}
}
println(r)
benkuly
09/17/2021, 11:58 AM