How do I get the content of a multipart form data ...
# ktor
v
How do I get the content of a multipart form data part? If I try like in the docs with
part.provider().readRemaining().readByteArray()
I only get an empty array, while I know for sure that there is content.
a
The listed code should work. Can you inspect the response body with a traffic-analyzing tool to determine if the part is empty?
v
See the last half-sentence. :-) I checked with logging
call.receiveText()
.
a
Can you share the URL of the endpoint if it's a part of a public service?
v
Ah, found the problem, thanks for rubber-ducking. 🙂
I did
Copy code
val parts = call
    .receiveMultipart()
    .asFlow()
    .toList()
    .map {
        when (it) {
            is PartData.FileItem -> {
                it.name to it.provider().readRemaining().readText()
            }

            else -> {
                logger.error { "Unexpected part data ${it::class.simpleName}" }
                call.respondText(
                    text = HttpStatusCode.InternalServerError.description,
                    status = HttpStatusCode.InternalServerError,
                )
                return@post
            }
        }
    }
    .associate { it }
for being able to to the
return@post
. I need to do the
map
still in the flow and
toList
after that, then it works and have to determine after that whether to
return@post
.