Add Bearer Token Selectively To Header In Retrofit
I would like to know if there is any option to enable/disable header Interceptor based on a flag or annotation in Retrofit. Since there are few paths in my API that do not need a t
Solution 1:
I have researched this some time ago and I didn't find any solution provided by retrofit out of the box, but you can easily implement a workaround by yourself.
Just annotate the request with a header of your choosing, for example "NoAuth: true" and check for this header in the interceptor:
builder.addInterceptor(chain -> {
StringnoAuthHeader= chain.request().header("NoAuth");
Request.Builderrequest= chain.request().newBuilder();
if(noAuthHeader == null || !noAuthHeader.equals("true")){
request.addHeader("authorization", "Bearer foofootoken").build();
}
return chain.proceed(request.build());
});
Post a Comment for "Add Bearer Token Selectively To Header In Retrofit"