If-None-Match doesn't get passed in my request

Viewed 965

I have seen good long discussion on this topic and it is claimed to be fixed in 2.3.0. Here is the combination I am using

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'

compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

compile 'com.squareup.okhttp3:okhttp:3.2.0'

logs I see against received response, have Etag; but subsequent request I do doesn't have If-None-Match passed in its header. I tested it by inserting If-None-Match explicitly by my code, caching worked and response was expected one. So there is surely something wrong with version of libraries I am using or something not good about my code.

Here I am setting up okClient.

HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);

        okhttp3.OkHttpClient okClient = new okhttp3.OkHttpClient.Builder()
                .addInterceptor(new HeaderInterceptor())
                .addInterceptor(httpLoggingInterceptor)
                .cache(createCacheForOkHTTP())
                .connectTimeout(5, TimeUnit.MINUTES)
                .readTimeout(5, TimeUnit.MINUTES)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(AppConfig.API_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okClient)
                .build();

My header interceptor contains the logic that is pretty much focused to my API itself. Here it is

private class HeaderInterceptor
            implements Interceptor {


        private String generateAuthHeader(AuthResponse accessToken) {
            if (accessToken == null) {
                return "";
            }
            return String.format("Bearer %s", accessToken.getAccessToken());
        }

        @Override
        public okhttp3.Response intercept(Chain chain)
                throws IOException {
            Request request = chain.request();
            final String authorizationValue = generateAuthHeader(runtime.getPrefAccessToken());
            if (!TextUtils.isEmpty(authorizationValue)) {
                request = request.newBuilder()
                        .addHeader(AppConfig.API_KEY_AUTHORIZATION, authorizationValue)
                        .addHeader(AppConfig.API_KEY_ACCEPT, AppConfig.API_ACCEPT)
                        .build();
                //.addHeader("If-None-Match", "a69385c6d34596e48cdddd3ce475d290")
            } else {
                request = request.newBuilder()
                        .addHeader(AppConfig.API_KEY_CONTENT_TYPE, AppConfig.API_CONTENT_TYPE)
                        .addHeader(AppConfig.API_KEY_ACCEPT, AppConfig.API_ACCEPT)
                        .build();
            }
            okhttp3.Response response = chain.proceed(request);
            return response;
        }

    }

And here is the method using which I am setting up cache.

 private Cache createCacheForOkHTTP() {
        Cache cache = null;
        cache = new Cache(App.getInstance().getBaseContext().getCacheDir(), 1024 * 1024 * 10);
        return cache;
    }

Looking for some quick and effective response as I already have spent reasonable time finding the solution but no luck.

Thanks

1 Answers
Related