Kotlin 接收到Ktor服务器的表单参数

Kotlin 接收到Ktor服务器的表单参数,kotlin,ktor,Kotlin,Ktor,我不熟悉Java和Kotlin,试图用Ktor构建一个联系人表单,因此我启用了gmail的不安全连接,并构建了以下应用程序: blogApp.kt: package blog import org.jetbrains.ktor.netty.* import org.jetbrains.ktor.routing.* import org.jetbrains.ktor.application.* import org.jetbrains.ktor.features.* import org.jet

我不熟悉Java和Kotlin,试图用
Ktor
构建一个联系人表单,因此我启用了gmail的不安全连接,并构建了以下应用程序:

blogApp.kt

package blog

import org.jetbrains.ktor.netty.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.features.*
import org.jetbrains.ktor.host.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*

import org.apache.commons.mail.*

fun Application.module() {
    install(DefaultHeaders)
    install(CallLogging)
    install(Routing) {
        get("/") {
            call.respondText("""
            My Example Blog2
                <form action="/contact-us" method="post">
                    <input name="subject" placeholder="Subject">
                    <br>
                    <textarea name="message" placeholder="Your message ..."></textarea>
                    <br>
                    <button>Submit</button>
                </form>
            """, ContentType.Text.Html)
        }
        post("/contact-us") {
            SimpleEmail().apply {
                setHostName("smtp.gmail.com")
                setSmtpPort(465)
                setAuthenticator(DefaultAuthenticator("my_alias@gmail.com", "my_gmil_passoword"))
                setSSLOnConnect(true)
                setFrom("my_alias@gmail.com")
                setSubject("subject")        // I need to use formParam
                setMsg("message")            // I need to use formParam
                addTo("my_alias@gmail.com")
            }.send() // will throw email-exception if something is wrong
            call.respondRedirect("/contact-us/success")
        }
        get("/contact-us/success") { 
            call.respondText("Your message was sent", ContentType.Text.Html) 
        }
    }
}

fun main(args: Array<String>) {
    embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}
事情进展顺利,我可以给自己发送一封电子邮件,然后重定向到成功页面,但发送的消息带有预设数据:

        setSubject("subject")        // I need to use formParam
        setMsg("message")            // I need to use formParam

如何使
Ktor
接收用户在表单中真正输入的数据,如何读取表单参数?

您可以使用
call.receive()
并反思数据:

import org.jetbrains.ktor.request.*     // for recieve
import org.jetbrains.ktor.util.*       // for ValuesMap

        post("/contact-us") {
            val post = call.receive<ValuesMap>() 
            val subj = post["subject"]
            val msg  = post["message"]

            SimpleEmail().apply { ... }
        }

虽然当时上述答案是正确的,但今天当我通过kotlin+Ktor时,我发现上述答案不再有效

您现在需要的是这样的东西:

  • call.receive()[“PARAM_NAME”]
Parameters类位于以下包中:io.ktor.http.Parameters

Nikhil

谢谢,您能在这里帮忙吗:
import org.jetbrains.ktor.request.*     // for recieve
import org.jetbrains.ktor.util.*       // for ValuesMap

        post("/contact-us") {
            val post = call.receive<ValuesMap>() 
            val subj = post["subject"]
            val msg  = post["message"]

            SimpleEmail().apply { ... }
        }
 val post = call.receiveParameters()