How can I add request interceptor in Retrofit 2.0, I looked at OKHttp but I found that to add request interceptor I have to actually create the request myself and if I create the request and set it in OkHttp how can this be used with Retrofit?
For logging I did that:
OkHttpClient client = new OkHttpClient();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client.interceptors().add(interceptor);
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
But I'm not sure how can I achieve this for the Request in order to add custom headers,
Another question (may be there's a better architecture), I have an API with 5 end-points and I have 1 abstract class where the client is built, then there are 5 different classes each one is based on 1 end-point, and they're all extending the abstract class where the client is constructed, in my old code with Retrofit 1, the abstract class had a request interceptor and is aware of the app's context, and populates the headers dynamically (which includes a checksum for the request, based on the parameters) how can I achieve this without request interceptor? and without have to repeat the headers for each end-point ?
I already looked at this:
https://github.com/square/okhttp/wiki/Interceptors
But I don't get how can add this interceptor without creating the request, and If I created the request myself, how can I pass this to Retrofit? or what's the use of retrofit then?
I also tried the following code:
retrofit.client().interceptors().add(new Interceptor()
{
@Override
public Response intercept(Chain chain) throws IOException
{
Request newRequest = chain.request().newBuilder()
.addHeader("appName", BuildConfig.APPLICATION_ID)
.addHeader("platform", "android")
.addHeader("appVersion", BuildConfig.VERSION_NAME)
.addHeader("token", "SecretToken")
.addHeader("Secret","not-secret-yet")
.build();
return chain.proceed(newRequest);
}
});
based on this answer: http://stackoverflow.com/questions/33558352/retrofit-2-addqueryparam-replacement
But it didn't seem to do anything, and seems that this request with its headers are overridden during the call time of the request.
After a lot of trials, one thing solved my problem I didn't deep into the code, but what I add HttpLoggingInterceptor before my custom Interceptor, the custom is ignored completely, so I had to flip them, does this make any sense?
Yes. Interceptors are ordered. An earlier one cannot see what a later one
does.
On Wed, Nov 18, 2015, 6:12 PM Mohamed Shehab [email protected]
wrote:
Closed #1299 https://github.com/square/retrofit/issues/1299.
—
Reply to this email directly or view it on GitHub
https://github.com/square/retrofit/issues/1299#event-468583859.
ok, this makes sense then.
so the headers were being sent but it's just that the logger didn't see this?
could be also useful if you could add this hint at any point in the docs, I know it's mainly about OKHttp now, but I believe that many people using retrofit will add at least the logging Interceptor. and if it's not the last, they'd go round in circles like me.
Most helpful comment
ok, this makes sense then.
so the headers were being sent but it's just that the logger didn't see this?
could be also useful if you could add this hint at any point in the docs, I know it's mainly about OKHttp now, but I believe that many people using retrofit will add at least the logging Interceptor. and if it's not the last, they'd go round in circles like me.