2017-08-27 60 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

它適合我,謝謝你! –