Hello, I'm migrating from retrofit to ktor on andr...
# ktor
k
Hello, I'm migrating from retrofit to ktor on android to enable sharing code with ios. I've done nearly everything, but I have got following problem with migration (Previously we were using retrofit interface with annotation and interceptor): • some calls require additional authentication • I want to add proper header with token to these calls • I case of 401, I want to perform additional call to refresh token and then retry repeat original call with updated token What fit best to these requirements? Should I write custom HttpClientFeature, somehow intercept such call with HttpSend, or anything else?
this is somehow similar to https://kotlinlang.slack.com/archives/C0A974TJ9/p1720886361005409 but I have a bit more generic problem
hmm, maybe this is the response https://ktor.io/docs/client-request-retry.html ?
is it ok, to add some attribute to call and check in
HttpRequestRetry
if such call meets criteria of authorization needed?
a
Have you tried to use the Auth plugin with Bearer authentication, which adds the
Authorization
header and refreshes the token on 401?
k
how to differentiate calls requiring and not requiring auth? Do I have to write to http clients, or can I do it in one (sometimes I have also optional authentication)
it would be simlest for me if I could use for example attributes (if I'm understanding them correct as they are some tags for calls)
a
By default, the client makes requests without the
Authorization
header and only adds the header after the server responds with 401. You can use the sendWithoutRequest method to configure which requests must include the
Authorization
header initially.
k
hmm, seems to be enough for my case, thanks very much!