guenhter
guenhter

Reputation: 12167

Ktor unable to receive POST body on Server

I have the following Server and Client code:

Server:

fun Application.main() {
    install(DefaultHeaders)
    install(CallLogging)
    install(Routing) {
        post("/") {
            val requestBody = call.receiveText()
            println("Received $requestBody")
            call.respond("Hello from server - received $requestBody")
        }
    }
}

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080) {
        main()
    }.start(wait = true)
}

Client:

fun main(args: Array<String>) = runBlocking {
    HttpClient(CIO).use {
        val postResult = it.post<String>("http://localhost:8080/") {
            body = "Client Hello"
        }
        println(postResult)
    }
}

So, the client just sends "Client Hello" to the server in the POST-body, and the Server responds to that. But I didn't see the content of the body on the server-side. What am I doing wrong?

The call.receiveText() is always empty.

Upvotes: 1

Views: 7611

Answers (2)

Rafael Baptista
Rafael Baptista

Reputation: 346

And if you have parameters in your body request, try it:

val requestBody = call.receiveParameters()
val value = requestBody["key_name"]

Upvotes: 1

Leonid
Leonid

Reputation: 464

The issue fixed in recent alphas(>= 0.9.2-alpha-5) and would appear in next 0.9.2 release soon.

Upvotes: 2

Related Questions