Is there a way to have the client rebuild a url wh...
# ktor
Is there a way to have the client rebuild a url when establishing a web socket connection? My company requires the token to be in the query param because browsers can't use headers for a web socket. The AuthProvider refreshes the token successfully, but then uses the same url on the retry connection, which causes it to fail with a 401 response again.
You can send the reference to a request to a channel and then receive it in the
lambda to modify it before sending retried request. Here is an example:
Copy code
import io.ktor.client.*
import io.ktor.client.engine.apache.*
import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.auth.providers.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

suspend fun main(): Unit = coroutineScope {
    val requestChannel = Channel<HttpRequestBuilder>()

    val client = HttpClient(Apache) {
        install(Auth) {
            bearer {
                loadTokens {
                    BearerTokens("", "")
                refreshTokens {
                    val context = requestChannel.receive()
                    // Here goes a code for getting a new token
                    val newToken = "123"
                    context.parameter("auth", newToken)
                    BearerTokens(newToken, "")

    client.get {
        url {
            host = ""
            port = 3030

        launch {
This solution isn’t optimal but should work even for Websockets requests.
👍 1