Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add RequestInterceptor in Retrofit 2.0? #1299

Closed
shehabic opened this issue Nov 18, 2015 · 4 comments
Closed

How to add RequestInterceptor in Retrofit 2.0? #1299

shehabic opened this issue Nov 18, 2015 · 4 comments

Comments

@shehabic
Copy link

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?

@shehabic
Copy link
Author

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.

@shehabic
Copy link
Author

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?

@JakeWharton
Copy link
Collaborator

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 #1299.


Reply to this email directly or view it on GitHub
#1299 (comment).

@shehabic
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants