Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit 2.x : Log Header for request and response

I am using retrofit 2.x and i want to log the header and body of request and response .

  HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
            .addNetworkInterceptor(new Interceptor() {
                @Override
                public okhttp3.Response intercept(Chain chain) throws IOException {
                    Request request = chain.request().newBuilder()
                            .addHeader("key", "value")
                            .addHeader("HEADER","HEADER Value")
                            .build();
                    return chain.proceed(request);
                }


            }).build();

And this how i am doing,my problem is header of request are not being logged in Android Monitor but rest everything is logged .

Gradle Version

 compile ('com.squareup.retrofit2:retrofit:2.0.0-beta3') {
    // exclude Retrofit’s OkHttp peer-dependency module and define your own module import
    exclude module: 'okhttp'
}
compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'
compile ('com.squareup.okhttp3:logging-interceptor:3.0.1'){
    exclude module: 'okhttp'
}

Using RC1 and 3.0.1 due to bug issue reported Bug Link

like image 312
Code_Life Avatar asked Feb 08 '16 09:02

Code_Life


People also ask

What is HttpLoggingInterceptor?

With a HttpLoggingInterceptor, OkHttp will automatically log incoming and outgoing HTTP requests and responses to Logcat, where we can then see information like the type of request, the fully resolved URL, the content-type, the different HTTP headers, and the payload of the body itself, which contains the actual JSON, ...


2 Answers

Oh I found the error if anyone is interested :

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
        .addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request().newBuilder()
                        .addHeader("key", "value")
                        .addHeader("HEADER","HEADER Value")
                        .build();
                return chain.proceed(request);
            }


        }).build();

You must add the log interceptor (your interceptor variable) after the request interceptor, so the correct answer is:

 HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
            @Override
            public okhttp3.Response intercept(Chain chain) throws 
 IOException {
                Request request = chain.request().newBuilder()
                        .addHeader("key", "value")
                        .addHeader("HEADER","HEADER Value")
                        .build();
                return chain.proceed(request);
            }


        })
        .addInterceptor(interceptor)
        .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR)
        .build();
like image 155
diegoveloper Avatar answered Oct 19 '22 23:10

diegoveloper


Instead of using addInterceptor to add the logging interceptor, use addNetworkInterceptor, to include headers added by OkHttp.

Network interceptors are able to:

Observe the data just as it will be transmitted over the network.

like image 24
reacuna Avatar answered Oct 19 '22 22:10

reacuna