Ktor Server: How do I read the body of a PartData?...
# ktor
h
Ktor Server: How do I read the body of a PartData?
Copy code
post {
  val part = call.receiveMultipart().asFlow().single()
  require(part is PartData.FileItem)
  val bytes = ByteArray(part.headers[ContentLength]!!.toInt())
  try {
    part.provider().readFully(bytes)
  } finally {
    part.dispose()
  }
}
I always get an closed
ByteReadChannel
.
Okay, found the issue: you should not call
asFlow().single()
because the body of the PartData is already closed. IMHO in that case you should not expose
asFlow()
, and
PartData
should be
AutoCloseable
👍 1
s
Alternative could be renamed to
consumeAsFlow
.
h
There already is a
forEachPart
but exposing a
Flow
could be questioned. Or add a warning that the body is only available inside the flow, because the flow terminates when
readPart()
returns null and
readPart()
returns null when the request is fully consumed...
a
This api has bitten me too in the past. There is something unexpected about it