I am using a base URL like below
private const val BASE_URL = "anyURL"
an htt*P client
private val client = OkHtttpClient.Builder().apply {
addInterceptor(MyInterceptor())
}.build()
a retrofit client
private val retrofit = Retrofit.Builder()
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.client(client)
.baseUrl(BASE_URL).build()
and a get call
@GET("playlist_tracks=*********")
suspend fun geAlltracks(): Music
Currently, I am using an interceptor, but how can append my request with headers without Interceptor


Rajkiran SwainRank #165 · 14.4K reputationPosted Mar 30, 2023, 2:32 AM EDT
You can append headers in Retrofit API call without using an Interceptor by creating a custom OkHttpClient with headers and using it to build the Retrofit instance. Here is an example of how you can do that:
private val headers = mapOf( "Header1" to "Value1", "Header2" to "Value2" ) private val client = OkHttpClient.Builder().apply { headers.forEach { (key, value) -> addHeader(key, value) } }.build()private val retrofit = Retrofit.Builder() .addConverterFactory(Json.asConverterFactory("application/json".toMediaType())) .client(client) .baseUrl(BASE_URL).build()@GET("playlist_tracks=*********") suspend fun geAlltracks(): MusicIn this way, the custom OkHttpClient will automatically append the headers to every API call made by the Retrofit instance.