Why does tag() coming null in Retrofit OkHttpInterceptor?

Viewed 16

Please see the code:

Interface:

/**
 * This class is specific for entire cart module only
 */
interface ICartAPIService {
    @GET(URLConstants.API_VERSION_V1 + URLConstants.API_URL_ADDTOCART)
    suspend fun getCartData(@Tag test: String, @Query(URLConstants.API_URL_CUSTOMER_ID_1) value: String): Response<CartResponse>
}

Calling from DataSource module:

apiService.getCartData("mTag", getCustomerId())

Interceptor:

override fun intercept(chain: Interceptor.Chain): Response {
    val request: Request = chain.request()
    val response = chain.proceed(request)

    LogUtils.d("Hello tag: " + request.tag(Invocation::class.java))
    LogUtils.d("Hello tag: " + request.tag())

Result:

Hello tag: com.abc.service.remote.cart.ICartAPIService.getCartData() [mTag, 5448852]

Hello tag: null

Expected output:

Hello tag: mTag

1 Answers

Solved like this:

val invocation: Invocation? = request.tag(Invocation::class.java)
    if ((invocation?.arguments()?.size ?: 0) > 0) {
        LogUtils.d("Hello test: " + invocation?.arguments()?.get(0))
    }
Related