bwnuk
bwnuk

Reputation: 69

Ktor - post unhanldled error with coroutines

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.

Upvotes: 5

Views: 2263

Answers (2)

Koch
Koch

Reputation: 594

I've experienced the same issue after upgrading to ktor 1.4.2 and Kotlin 1.4.20, and I used both Moshi and Gson on this specific project but I don't believe they are causing this issue.

If you have a 'gradle.properties' file, add these ( or whatever version you wish to use ) :

ktor_version=1.3.2 

kotlin_version=1.3.70.

Otherwise, in your 'build.gradle' file, create variables for different version :

buildscript {

ext.kotlin_version = '1.3.70'
ext.ktor_version = '1.3.2'
repositories {
    jcenter()
}

dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}

}

Then sync your gradle, run project.. all should be good. However if you still experience some gradle-related issue, try this :

go to gradle (folder) -> wrapper -> open gradle_wrapper.properties and make sure the url has version 6.x.x or 5.x.x.

Mine looks like this currently:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

Upvotes: 0

shadowsheep
shadowsheep

Reputation: 15012

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.

Upvotes: 7

Related Questions