Retrofit 2.x : Log Header for request and response
You need to set following:
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
clientBuilder.addNetworkInterceptor(httpLoggingInterceptor);
clientBuilder.build()
May it help someone ...
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
Add both to see complete logs and add this interceptor at the last (don't know why but its like this).
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.
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();