I'd like to intercept all responses received by the retrofit engine, and scan for HTTP error code, for example error 403.
I'm aware I can use the failure(RetrofitError error) callback of every request and check for 403's but I'd like to wrap the response globally.
I can see that request interception is possible, but I do not see a similar option for response.
Any suggestions?
We can use interceptors to do a variety of things, such as centrally monitoring API calls. In general, we need to add a logger for each network call, but by using the interceptor, we can add a logger once and it will work for all network calls.
What are Interceptors? According to documentation, Interceptors are a powerful mechanism that can monitor, rewrite, and retry the API call. So basically, when we do some API call, we can monitor the call or perform some tasks.
The Chain object in retrofit is an implementation of the Chain of Responsibility design pattern, and each interceptor is a processing object which acquires the result of the previous interceptor through chain.
header("User-Agent", "OkHttp Headers. java") . addHeader("Accept", "application/json; q=0.5") . addHeader("Accept", "application/vnd.
I was able to accomplish that by adding an interceptor to the OkHttpClient that retrofit is using.
Kotlin + Retrofit 2.x
val clientBuilder = OkHttpClient.Builder() clientBuilder.addInterceptor { chain -> val request = chain.request() val response = chain.proceed(request) if (response.code() == 403) { handleForbiddenResponse() } response }
Retrofit 2.x:
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); clientBuilder. addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); if (response.code() == 403) { handleForbiddenResponse(); } return response; } }); Retrofit retrofit = new Retrofit.Builder() .baseUrl(API_BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .client(clientBuilder.build();) .build();
Retrofit 1.x:
public class ForbiddenInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = chain.proceed(request); if (response.code() == 403) { handleForbiddenResponse(); } return response; } } OkHttpClient okHttpClient = Utils.createUnsafeOkHttpClient(); okHttpClient.interceptors().add(new ForbiddenInterceptor()); RestAdapter.Builder restAdapterBuilder = new RestAdapter.Builder() .setEndpoint(API_BASE_URL) .setClient(new OkClient(okHttpClient));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With