我有以下服务器和客户端代码:
服务器:
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)
}
客户:
fun main(args: Array<String>) = runBlocking {
HttpClient(CIO).use {
val postResult = it.post<String>("http://localhost:8080/") {
body = "Client Hello"
}
println(postResult)
}
}
因此,客户端只需在 POST 正文中向服务器发送“Client Hello”,服务器就会对此做出响应。但是我没有在服务器端看到正文的内容。我究竟做错了什么?
call.receiveText()
总是空的。