Hello guys is there any way to add headers to all ...
# ktor
p
Hello guys is there any way to add headers to all request in Ktor? I've tried to add them via
buildHeaders { append("foo","bar" }
but looks like is not working if I add them in the
request
it works... it's weird.
Copy code
httpClient.get<FooResponse>("<https://foo.bar>"){header("foo","bar")} <-- it works
Any idea why?
d
Yes, with a feature.
I think
defaultRequest
could probably do it.
o
Yep, defaultrequest will do
https://ktor.io/docs/default-request.html#installation It is the canonical example in the documentation
Just be aware of the fact that
HttpRequestBuilder.header()
function appends the value if the header is already set. https://youtrack.jetbrains.com/issue/KTOR-2492
p
Ok thanks guys that was the thing
Thanks for the clarification 🙂
Is there any way to verify that it's adding the headers? I have this
Copy code
engine {
                val loggingInterceptor = HttpLoggingInterceptor()
                loggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC
                addInterceptor(loggingInterceptor)
            }
I've tried to use Level.HEADERS but don't see them