Hi. I have a KTOR server, and I want a single end...
# ktor
l
Hi. I have a KTOR server, and I want a single endpoint to receive either JSON or multipart-form. I thought I could detect it like so:
Copy code
when (val contentType = call.request.contentType()) {
    ContentType.Application.Json -> {
        call.receive<etc>()
    }
    ContentType.MultiPart.FormData -> {
        etc.decode(call.receiveMultipart())
    }
    else -> {
        call.respond(HttpStatusCode.BadRequest, "ContentType not supported: $contentType")
        return@post
    }
}
It's hitting the else case, with the content type being
multipart/form-data; boundary=----geckoformboundary72272ecba2803550f625913948b580af
How am I meant to properly detect if it's multipart or not?
a
You can switch on the
Content-Type
without parameters to make one of the conditions succeed:
Copy code
post {
    when (call.request.contentType().withoutParameters()) {
        ContentType.MultiPart.FormData -> {
            println("Form data")
        }
    }
}
l
Ahh, thanks 🙂