Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to absolute URL in Ktor

Tags:

kotlin

ktor

I'm learing how to work with ktor. For a special use case i need to redirect to a different domain from my ktor server. However i cant get it to work rigth now.

As an simple example i have an Application.kt

import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*

fun main(args: Array<String>) {
    val server = embeddedServer(Netty, port = 8080) {
        routing {
         get("/") {
           call.respondRedirect("www.google.com")
        }}
    }
}
server.start(wait = true)

}

What i except is that it redirects me to www.google.com however it redirects me to localhost:8080/www.google.com

like image 886
member2 Avatar asked Oct 14 '25 09:10

member2


1 Answers

Figured it out. You need to set also the protocol. This works

call.respondRedirect("https://google.com/")
like image 63
member2 Avatar answered Oct 18 '25 03:10

member2