https://kotlinlang.org logo
Title
n

natario1

06/16/2021, 11:07 AM
👋 is there any way to change the response headers? Server is not sending
WWW-Authenticate
on 401, so the Auth plugin is not working properly. I have tried with:
client.receivePipeline.intercept(HttpReceivePipeline.Before) { response ->
    if (response.status == HttpStatusCode.Unauthorized && !response.headers.contains(HttpHeaders.WWWAuthenticate)) {
        val headers = buildHeaders {
            appendAll(response.headers)
            append(HttpHeaders.WWWAuthenticate, "Bearer")
        }
        proceedWith(object : HttpResponse() {
            override val headers get() = headers
            // delegate all other properties to original response
        })
    } else {
        proceed()
    }
}
And I can see with the debugger that this is invoked before the header check in Ktor's
Auth
class. But still, the response passed to Auth does not include my header. It seems like the response passed to
proceedWith()
does not replace
call.response
, which is what Auth plugin reads. Any idea?
r

Rustam Siniukov

06/16/2021, 11:28 AM
Unfortunately, there is no way to do it now.
receivePipeline
semantics is broken, because its context is
HttpClientCall
that has reference on response, that you can’t update. We will fix it in 2.0.0. For now, you can only fix your backend or copy-paste
Auth
feature so that it doesn’t need
WWW-Authenticate
header. Sorry =(
a

Aleksei Tirman [JB]

06/16/2021, 11:39 AM
n

natario1

06/16/2021, 11:46 AM
What about OkHttp network interceptor? Would that be invoked before Ktor response is parsed?
r

Rustam Siniukov

06/16/2021, 11:54 AM
Yes, it will.
n

natario1

06/16/2021, 11:57 AM
Great! I'll test this. Thanks for helping 🙏
r

Rustam Siniukov

06/16/2021, 3:02 PM