2017-08-27 17 views
-1

でポストデータを取得することはできません。これは私のコードです:Kotlin 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

、おかげであなたを! –

関連する問題