Hi all, when using ktor, how can i redirect to ano...
# ktor
b
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
As another option, you can use query parameters.
b
@Aleksei Tirman [JB] what is the best way to pass query parameter with redirect
call.respondRedirect("/detail")
a
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