https://kotlinlang.org logo
#ktor
Title
# ktor
b

brabo-hi

06/28/2022, 12:02 AM
Hi all, when using ktor, how can i redirect to another page and passing data? I have 2 pages (login and detail) and freemarker as template engine. On login page after form is submit, i would like to redirect to page detail
call.respondRedirect()
instead of rendering the new page
call.respondTemplate('detail.ftl', mapOf("myData" to myValue))
however i need to send some data to the new page when rendering the template
Copy code
post("/login") {
            // Check everything is correct
            val isSuccess = myRepository.process(username, password)
            
            if (isSuccess) {
                val uiMap = mapOf("key" to "value")
                call.respondTemplate("detail", uiMap)
            
            // Bellow is what i want to do, but i need to pass uiMap 
            // call.respondRedirect("/detail")
            } else {
                val uiMap = mapOf("key" to "username or password incorrect")
                call.respondTemplate("detail", uiMap)
                // Bellow is what i want to do, but i need to pass uiMap 
                // call.respondRedirect("/login")
            }
        }
PS: i don’t want to use session or cookie
a

Aleksei Tirman [JB]

06/28/2022, 7:53 AM
As another option, you can use query parameters.
b

brabo-hi

06/28/2022, 10:19 PM
@Aleksei Tirman [JB] what is the best way to pass query parameter with redirect
call.respondRedirect("/detail")
a

Aleksei Tirman [JB]

06/29/2022, 8:13 AM
You can pass them in the following way:
Copy code
get("/redirect") {
    val query = URLBuilder().apply {
        parameters.append("param1", "value1")
        parameters.append("param2", "value2")
    }.build().encodedQuery

    call.respondRedirect("/?${query}")
}
1
15 Views