Respond redirect

Report a typo

Sometimes when creating sites you will change the URL in the router handler. But in this case the old links to this page will no longer work. In such cases, it is advisable to redirect the user from the old URL to the new one. Ktor has a call.respondRedirect function for this purpose. Suppose you have a simple redirect from /oldurl to /newurl

get("/oldurl") {
    call.respondRedirect("/newurl", permanent = true)
}
get("/newurl") {
    call.respondText("Hello, user!")
}

Actually, the call.respondRedirect function just sends the client a header that tells the browser to redirect. Use Postman to request /oldurl and see what header the call.respondRedirect function sends to the client. The header you are looking for will contain the path /newurl to which the request is redirected.

Tip: To view response headers, open the Postman console, select the corresponding request (in our case /oldurl) and find the desired response header there.consle button in the bottom of a postman window

Enter a short text
___

Create a free account to access the full topic