Kotlin Ktor可以';无法使用位置数据类获取post数据
这是我的代码:Kotlin Ktor可以';无法使用位置数据类获取post数据,kotlin,web-frameworks,ktor,Kotlin,Web Frameworks,Ktor,这是我的代码: package com.example.ktordemo import org.jetbrains.ktor.application.Application import org.jetbrains.ktor.application.install import org.jetbrains.ktor.application.log import org.jetbrains.ktor.auth.UserHashedTableAuth import org.jetbrains.kt
package com.example.ktordemo
import org.jetbrains.ktor.application.Application
import org.jetbrains.ktor.application.install
import org.jetbrains.ktor.application.log
import org.jetbrains.ktor.auth.UserHashedTableAuth
import org.jetbrains.ktor.features.CallLogging
import org.jetbrains.ktor.features.ConditionalHeaders
import org.jetbrains.ktor.features.DefaultHeaders
import org.jetbrains.ktor.features.PartialContentSupport
import org.jetbrains.ktor.locations.*
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.util.decodeBase64
import org.slf4j.Logger
@location("/login")
data class Login(val userId: String = "", val password: String = "", val error: String = "")
@location("/userTable") class SimpleUserTable
val hashedUserTable = UserHashedTableAuth(table = mapOf(
"test" to decodeBase64("VltM4nfheqcJSyH887H+4NEOm2tDuKCl83p5axYXlF0=")
))
fun Application.basicAuth() {
install(DefaultHeaders)
install(CallLogging)
install(ConditionalHeaders)
install(PartialContentSupport)
install(Locations)
install(Routing) {
manual(log)
}
}
fun Route.manual(log: Logger) {
post<Login> {
log.info(it.toString())
call.respond(it.userId) // get nothing
}
get { login: Login ->
call.respond("login page")
}
}
package com.example.ktordemo
导入org.jetbrains.ktor.application.application
导入org.jetbrains.ktor.application.install
导入org.jetbrains.ktor.application.log
导入org.jetbrains.ktor.auth.UserHashedTableAuth
导入org.jetbrains.ktor.features.CallLogging
导入org.jetbrains.ktor.features.ConditionalHeaders
导入org.jetbrains.ktor.features.DefaultHeaders
导入org.jetbrains.ktor.features.PartialContentSupport
导入org.jetbrains.ktor.locations*
导入org.jetbrains.ktor.response*
导入org.jetbrains.ktor.routing*
导入org.jetbrains.ktor.util.decodeBase64
导入org.slf4j.Logger
@位置(“/login”)
数据类登录(val userId:String=“”,val password:String=“”,val error:String=“”)
@位置(“/userTable”)类SimpleUserTable
val hashedUserTable=UserHashedTableAuth(table=mapOf(
“测试”到decodeBase64(“VltM4nfheqcJSyH887H+4NEOm2tDuKCl83p5axYXlF0=“)
))
有趣的应用程序{
安装(默认标题)
安装(调用日志记录)
安装(条件标头)
安装(部分内容支持)
安装(位置)
安装(路由){
手册(日志)
}
}
有趣的路线。手册(日志:记录器){
职位{
log.info(it.toString())
call.respond(it.userId)//什么也得不到
}
获取{login:login->
call.respond(“登录页面”)
}
}
我使用失眠测试请求,这是结果:
> POST /login HTTP/1.1
> Host: localhost:8080
> User-Agent: insomnia/5.6.3
> Accept: */*
> Accept-Encoding: deflate, gzip
> Content-Type: application/x-www-form-urlencoded
> Content-Length: 33
| userId=username&password=password
< HTTP/1.1 200 OK
< Date: Sun, 27 Aug 2017 16:52:20 GMT
< Server: ktor-core/0.4.0 ktor-core/0.4.0
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 0
>发布/登录HTTP/1.1
>主机:本地主机:8080
>用户代理:失眠/5.6.3
>接受:*/*
>接受编码:deflate,gzip
>内容类型:application/x-www-form-urlencoded
>内容长度:33
|userId=用户名&密码=密码
有人能帮我吗?以同样的方式处理所有参数(查询和发布),这是一个不幸的结果。这是固定的,现在应该显式接收post参数。这很有意义,因为位置是一个往返实体,就像您可以处理它一样,您可以从中生成一个URL,它应该命中同一个处理程序。使用POST参数是不可能的 现在,您需要
调用.receive()
并从映射中手动获取post参数。类型化绑定正在进行中
您可以在此处跟踪进度:这是以相同方式处理所有参数(查询和发布)的不幸结果。这是固定的,现在应该显式接收post参数。这很有意义,因为位置是一个往返实体,就像您可以处理它一样,您可以从中生成一个URL,它应该命中同一个处理程序。使用POST参数是不可能的 现在,您需要
调用.receive()
并从映射中手动获取post参数。类型化绑定正在进行中
您可以在此处跟踪进度: