Reputation: 145
I'm trying to add query parameter (access token) to every request with this tutorial
The problem are the interceptors, which count will accrue in every request created by ServiceGenerator:
httpClient.addInterceptor(new Interceptor() {
Also cause httpClient is static, all the interceptors will be executed in requests where it doesn't needed.
Should i create own OkHttpClients for normal request and own for token request? and should i initialize interceptor and authenticator only once and then use ServiceGenerator? or is there any better approach?
Upvotes: 1
Views: 952
Reputation: 1661
I think this is not a good idea to add access token as query parameter.
Better way will be to add it to a custom header like this:
okHttpClient = new OkHttpClient.Builder()
.cache(setCache(context))
.certificatePinner(certificatePinnerBuilder.build())
.retryOnConnectionFailure(false)
.readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Content-type", "application/json")
.header("AUTH_TOKEN", token)
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
})
.addInterceptor(logger)
.build();
However, you can follow this link:
https://futurestud.io/tutorials/retrofit-2-how-to-add-query-parameters-to-every-request
Upvotes: 2