https://kotlinlang.org logo
#ktor
Title
# ktor
p

Pablo

04/13/2021, 6:44 PM
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

Dominaezzz

04/13/2021, 6:58 PM
Yes, with a feature.
I think
defaultRequest
could probably do it.
o

okarm

04/13/2021, 7:02 PM
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

Pablo

04/13/2021, 7:44 PM
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
3 Views