面试题答案
一键面试在Ktor中,可以通过call.parameters
获取查询参数,通过call.receiveParameters()
获取表单参数。以下是示例代码:
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
fun Application.module() {
routing {
post("/example") {
// 获取查询参数
val queryParam = call.parameters["queryParam"]
// 获取表单参数
val formParameters = call.receiveParameters()
val formParam = formParameters["formParam"]
call.respondText("Query Param: $queryParam, Form Param: $formParam", contentType = ContentType.Text.Plain)
}
}
}
上述代码创建了一个Ktor应用,在/example
路径处理POST请求,分别获取查询参数queryParam
和表单参数formParam
,并返回给客户端。