Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ktor - post unhanldled error with coroutines

Tags:

kotlin

ktor

I a new to Kotlin and Ktor in particular, so I have tried to do simple post request. As you can see below, there is nothing special.

    routing {
    post("/articles/add"){
        val post = call.receive<ArticleRequest>()
        println(post)
    }

Error shown in logs is below and I don't understand why I should use here coroutines.

ERROR Application - Unhandled: POST - /articles/add 
java.lang.IllegalStateException: Using blocking primitives on this dispatcher is not allowed. Consider using async channel instead or use blocking primitives in withContext(Dispatchers.IO) instead.

I am using 1.4.2 version. I would appreciate any help.

like image 209
bwnuk Avatar asked Nov 18 '20 23:11

bwnuk


1 Answers

If you are using Jackson this is a bug and there is a suggested workaround:

routing {
    post("/articles/add") {
        with(Dispatchers.IO) {
            val post = call.receive<ArticleRequest>()
            println(post)
        }
    }
}

Or you can rollback to 1.4.1 until the bug is solved.

like image 50
shadowsheep Avatar answered Nov 01 '22 02:11

shadowsheep