TransWikia.com

OkHttpClient doesn't add headers to requests

Stack Overflow Asked by Ghaylen on December 21, 2020

I m creating an android app and i m using retrofit to consume api webservices. my problem is when i add a header using annotation, it works.

@Headers("Authorization: jwt-token")
@POST("appusers-service/signin/identification")
suspend fun authentication(@Body body : LoginIdentificationDto.Params) : NetworkResponse<LoginIdentificationDto.Result, LoginIdentificationDto.Errors>

but when I use intercepters to add the header dynamically, it doesn’t work

@Provides
@Singleton
fun provideHttpClient() : OkHttpClient
{
    return OkHttpClient.Builder()
        .callTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)
        .addNetworkInterceptor {
            
            val builder : Request.Builder = it.request().newBuilder()
            
            builder.addHeader(AUTHORIZATION_HEADER, "jwt-token")
            
            return@addNetworkInterceptor it.proceed(builder.build())
        }
        .build()
}

@Provides
@Singleton
fun provideRetrofit(httpClient : OkHttpClient, mapper : ObjectMapper) : Retrofit
{
    return Retrofit.Builder()
        .baseUrl(BASE_URL)
        .client(httpClient)
        .addCallAdapterFactory(NetworkResponseAdapterFactory())
        .addConverterFactory(JacksonConverterFactory.create(mapper))
        .build()
}

how can i solve this problem please?

2 Answers

The way I'm doing is to have a class AuthInterceptor

class AuthInterceptor : Interceptor {

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val authenticatedRequest = request.newBuilder()
            .addHeader("Content-Type", "application/json")
            .addHeader("Authorization", UserData.getAuthToken())
            .build()
        return chain.proceed(authenticatedRequest)
    }
}

And on your OkHtttpClient use like this.

val ohHttpClient = OkHttpClient.Builder()
ohHttpClient.addInterceptor(AuthInterceptor())

Answered by Emilio Hoffmann on December 21, 2020

Try this one

return OkHttpClient.Builder()
        .callTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)
        .addInterceptor {
            val builder : Request.Builder = it.request().newBuilder()
            builder.addHeader(AUTHORIZATION_HEADER, "jwt-token")
            return@addNetworkInterceptor it.proceed(builder.build())
        }
        .build() 

Note that I have changed addNetworkInterceptor into addInterceptor. For more information you can refer to this answer https://stackoverflow.com/a/37120830/9394700

Answered by mirsaidoff on December 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP