0

I have an apikey "token" that has to be provided as a query parameter in every request. How can i setup retrofit as to always include this query parameter in the request?

I am currently providing the token as a default parameter:

interface CompanyService {

    @GET("accounts/{id}")
    fun getCompany(
            @Path("id") id: Number,
            @Query("token") token: String = Constants.TOKEN) <---- here
            : Call<CompanyResponse>

    @GET("accounts/")
    fun getCompanies(
        @Query("id") page: String,
        @Query("limit") limit: Int,
        @Query("sort") sort: String = "id",
        @Query("token") token: String = Constants.TOKEN) <---- here
            : Call<CompanyListResponse>
}

But since i know that every request requires the same token, it feels redundant to have to provide it as a default parameter in every request function i create. How can i setup retrofit to decorate every outgoing request with a default query parameter?

This is how i build the retrofit2 instance:

class CompanyAPI {
    companion object {
        private var retrofit: Retrofit? = null
        val client:Retrofit get() {
            if (retrofit == null) {
                retrofit=Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(Constants.BASE_URL)
                    .build()
            }
            return retrofit!!
        }
    }
}

I would think that you would add some additional function call in the call chain before calling .build() but looking at the available functions i can't say i see anything that would provide this functionality.

Tagor
  • 937
  • 10
  • 30

2 Answers2

2

You need to add an interceptor to it & pass the client in your Builder like the following:

retrofit=Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(Constants.BASE_URL)
    .client(
        OkHttpClient.Builder()
            .addInterceptor { chain ->
                val url = chain
                    .request()
                    .url()
                    .newBuilder()
                    .addQueryParameter("token", Constants.TOKEN)
                    .build()
                chain.proceed(chain.request().newBuilder().url(url).build())
            }
            .build()
    )
    .build()
SaadAAkash
  • 3,065
  • 3
  • 19
  • 25
1

add interceptor to your http client with your default params or headers here is example for how to add it :-

OkHttpClient.Builder httpClient =  
new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {  
@Override
public Response intercept(Chain chain)
throws IOException {
    Request original = chain.request();
    HttpUrl originalHttpUrl = original.url();

    HttpUrl url = originalHttpUrl.newBuilder()
            .addQueryParameter("apikey", "your-actual-api-key")
            .build();

    // Request customization: add request headers
    Request.Builder requestBuilder = original.newBuilder()
            .url(url);

    Request request = requestBuilder.build();
    return chain.proceed(request);
}
});

refrence