LoginSignup
0
0

OkHttpでステータスコードを変更する

Last updated at Posted at 2023-09-02

概要

OkHttpのAuthenticatorを使用して、ステータスコードが401の場合にアクセストークンを更新する処理を実装していました。しかし、アクセストークンが無効なエラーがステータスコードが400で返されることがあり、本来はAPIの仕様を変更してもらうのが適切な解決策ですが、その際にもクライアント側でアクセストークンを更新するようにしました。

対応方法

Authenticatorへはステータスコードが401の場合のみリクエストが到達するため、ステータスコードが400の場合に401に変更するInterceptorを適用しました。

class ReplaceStatusCodeInterceptor() : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val response = chain.proceed(chain.request())
        return if (response.code != 400) {
            response
        } else {
            response.newBuilder().code(401).build()
        }
    }
}

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0