使用ktor,dsl构建的form表单无法提交


#1

下面是代码

Application.kt

@KtorExperimentalLocationsAPI
@Location("/user")
class User {
    @Location("/login")
    data class UserLogin(val username: String, val password: String)

    @Location("/register")
    data class UserRegister(val username: String, val password: String)
}


fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@KtorExperimentalLocationsAPI
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
    install(Locations)

    routing {
        user()

        get("/") {
            call.respondHtml {
                head {
                    title {
                        +"Ktor 入门"
                    }
                }
                body {
                    a {
                        href = "/register"
                        +"注册"
                    }

                    form("/user/login", method = FormMethod.post, encType = FormEncType.multipartFormData) {
                        input {
                            type = InputType.text
                            name = "username"
                        }
                        br
                        input {
                            type = InputType.password
                            value = ""
                            name = "password"
                        }
                        br
                        input {
                            type = InputType.submit
                            value = "login"
                        }
                    }
                }
            }
        }

        get("/login") {
            call.respond("success")
        }

    }
}
@KtorExperimentalLocationsAPI
fun Route.user() {
    post<User.UserLogin> {
        call.respondText("登录成功")
    }

    post<User.UserRegister> { user ->
        run {
            call.respondText("用户 ${user.username} ${user.password}")
        }
    }
}

在form表单里提交信息只会 404

隐隐约约知道点原因

form表单post提交的地址是 /user/login 但是后端要接收的地址应该是 /user/login?username="xxx"&password="xxx"

所以单独测试的时候是正确的

是我使用方法不对,还是是个bug

最后实在不知道应该放到那个节点下,是不是应该在新建一个节点,kotlin框架啥的


京ICP备16022265号-2 Kotlin China 2017 - 2018
本站由腾讯云提供计算服务