I have a client with default headers ```defaultReq...
# ktor
s
I have a client with default headers
Copy code
defaultRequest {
            url("url")
            headers.appendIfNameAbsent("Authorization", "Bearer token")
}
and I am writing
Auth
to refresh the token . But on the refresh POST call, I don't want to pass the Authorization header. How do I remove it . Setting header to empty string or null, both don't work
Copy code
install(Auth) {
        bearer {
            loadTokens {
               ... load token
            }

            refreshTokens {
                val renewTokens = <http://client.post|client.post>("refreshtoken") {
                        headers.append("Authorization", "") // this doesn't work
                        header("Authorization", null). // this also doesn't work
                        markAsRefreshTokenRequest()
                    }.body()
            }
        }
    }
h
client.plugin(HttpSend).intercept { request -> }
You can check the request URL that matches your path and remove the headers
request.headers.remove(HttpHeaders.Authorization)
execute(request)
a
Have you tried removing the header with
headers.remove(HttpHeaders.Authorization)
?
s
yes tried. this also doesn't work
a
You can solve this problem by using an attribute to mark the refresh token request and then remove the header only for the marked requests in any phase after the
HttpRequestPipeline.Before
. Here is an example:
Copy code
val client = HttpClient(CIO) {
    defaultRequest {
        headers.appendIfNameAbsent("Authorization", "Bearer")
    }

    install(Auth) {
        bearer {
            refreshTokens {
                println("In refresh")
                val r = client.get("<https://httpbin.org/get>") {
                    markAsRefreshTokenRequest()
                }
                println(r.bodyAsText())
                BearerTokens("refreshed", "")
            }
        }


    }
}

client.requestPipeline.intercept(HttpRequestPipeline.State) {
    if (context.attributes.contains(AuthCircuitBreaker)) {
        context.headers.remove(HttpHeaders.Authorization)
    }
}

client.get("<https://httpbin.org/bearer>") {
    headers.remove(HttpHeaders.Authorization)
}
s
Hi, Thanks for it. It worked (didn't use the last part though)