https://kotlinlang.org logo
#ktor
Title
# ktor
s

Stephan Schroeder

02/12/2022, 8:54 PM
UPDATE: I got it running! I started from the ktor-serialisation example here https://github.com/ktorio/ktor-documentation/tree/main/codeSnippets/snippets/json-kotlinx and introduced my code. I'm not sure what I missed in the first place, but now it runs. So I'm trying out ktor and something in my setup must be wrong:
Copy code
fun main() {
    embeddedServer(Netty, port = 8080, host = "localhost") {
        configureRouting()
        configureSerialization()
    }.start(wait = true)
}

fun Application.configureSerialization() {
    install(ContentNegotiation) {
        json(Json {
            prettyPrint = true
            isLenient = true
        })
    }
}
fun Application.configureRouting() {
    routing {
        get("/") {
            call.respondText("Wolt Opening Times Formatter")
        }
        post("/") {
            val data = call.receive<String>()
            call.respondText(data.toString())
        }
    }
}
so the GET works and POSTing a String works as well, but as soon as I try to parse into something more ambicious, this fails, e.g. let's update the POST like this
Copy code
post("/") {
            val data = call.receive<TestData>()
            call.respondText(data.toString())
        }
    }
}
@kotlinx.serialization.Serializable
data class TestData(
    val monday: Int,
)
then sending over this json
Copy code
{
  "monday": 12
}
fails with
Copy code
Cannot transform this request's content to simonvoid.gmx.de.time_formatter.plugins.TestData
io.ktor.features.CannotTransformContentToTypeException: Cannot transform this request's content to simonvoid.gmx.de.time_formatter.plugins.TestData`
...
Caused by: io.ktor.features.CannotTransformContentToTypeException: Cannot transform this request's content to simonvoid.gmx.de.time_formatter.plugins.TestData
it's the same exception when I try to receive into a Map (
val data = call.receive<Map<String,Int>>()
).
Copy code
Caused by: io.ktor.features.CannotTransformContentToTypeException: Cannot transform this request's content to kotlin.collections.Map<kotlin.String, kotlin.Int>
a

Aleksei Tirman [JB]

02/14/2022, 9:05 AM
Could you please share request headers? What version of Ktor do you use?
s

Stephan Schroeder

02/15/2022, 10:32 AM
like i added to the update section at the top, i got it working. I'm just not sure which change made it work 😅 But for the future I've got a nice template project now 🙂