2017-08-27 62 views
-1

获得数据后这是我的代码:科特林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.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") 
    } 
} 

我用失眠测试的要求,这是结果:

> 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 

任何人都可以帮我吗?

回答

2

这是以同样的方式处理所有参数(查询和帖子)的不幸后果。这是固定的,现在应该明确地接收发布参数。它是有道理的,因为位置是一个往返实体,就像你可以处理它,你可以从它生成一个URL,它应该击中相同的处理程序。使用POST参数是不可能的。

现在您需要call.receive<ValuesMap>()并从地图手动获取发布参数。键入的绑定正在工作中。

您可以在这里跟踪进度:https://github.com/Kotlin/ktor/issues/190

+0

它适合我,谢谢你! –