https://kotlinlang.org logo
Title
m

Muh Rahmatullah

02/29/2020, 6:47 PM
Hi all, new to ktor, I am creating simple api using ktor and gson for json serialization. I tried to handle simple POST request which has json payload but I always got error when I wanted to retrieve the payload using
call.receive<Foo>()
and the error is
java.lang.UnsupportedOperationException: This function has a reified type parameter and thus can only be inlined at compilation time, not called directly.
this is the post handler:
fun Route.createMovie(movies: MutableList<Movie>) {
    post("/movie") {
        val id = Random.nextInt(1, 100000)
        println("header ${call.request.headers["Content-Type"]}")
        if(movies.find { it.id == id.toString() } != null) {
            // this is example to modify status code in the response and assign the value to the response wrapper
            call.response.status(value = HttpStatusCode.BadRequest)
            call.respond(Response(data = null, code = call.response.status()?.value, message = "Movie with $id already exist"))
        } else {
            val payload = call.receive<Movie>()
            println(payload)
            movies.add(payload)
            call.respond(Response(data = movies, code = 200, message = "OK"))
        }
    }
}
and my Application
fun Application.module(testing: Boolean = false) {

    val movies = initData()

    install(ContentNegotiation) {
        gson {
            setPrettyPrinting()
        }
    }

    routing {
        get("/") {
            call.respondText("A very simple rest api using k-tor!", contentType = ContentType.Text.Plain)
        }
        movies(movies)
        movieById(movies)
        deleteById(movies)
        createMovie(movies)
    }
}
Also, I already set the Content-type to application/json when sending the request. TIA, any help would be great
hmm, I think this is something because the reified type, I tried used other receive function which is
call.receive(Movie::class)
and now it works