I am trying to add bearer token in the API request but token not added.
Here is my code:
object APIClient {
val client: ApiInterface
get() {
val httpLoggingInterceptor =
HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer ${getString(TOKEN, "")}")
.build()
chain.proceed(request)
}
.addInterceptor(httpLoggingInterceptor)
.build()
val retrofit: Retrofit = retrofit2.Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
return retrofit.create(ApiInterface::class.java)
}
}
Below I have attacked response image for reference
I am trying to add bearer token in the API client but it's not added in the request.
no need to create instance here just need to return retrofit only
object API {
val client: Retrofit
get() {
val httpLoggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
val okHttpClient = OkHttpClient.Builder()
.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("Authorization", "Bearer ${getString(TOKEN, "")}")
.build()
chain.proceed(request)
}
.addInterceptor(httpLoggingInterceptor)
.build()
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
return retrofit
}
}