Issue
Is there a way to remove a specific header after setting this kind of Interceptor :
public class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request()
val request: Request = original.newBuilder()
.addHeader(AppConstant.HEADER_APP_TOKEN, AppConfig.apptoken) //<-- need to remove this one for only one request
.addHeader(AppConstant.HEADER_SECURITY_TOKEN, AppConfig.security_token)
.method(original.method(), original.body())
.build()
return chain.proceed(request)
Here is my Retrofit instance :
object RetrofitClientInstance {
private val httpClient = OkHttpClient.Builder()
.addInterceptor(AuthInterceptor())
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
private val retrofit = Retrofit.Builder()
.baseUrl(AppConstant.SERVER_BETA)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()
fun <T> getRetrofitInstance(service: Class<T>): T {
return retrofit.create(service)
}
}
And this is my API Service :
interface ApiService {
@GET("/app/shoes")
fun getShoes() : Call<Shoes>
}
Thanks for your help :)
Solution
Do it the other way around. Add a header to API calls indicating whether to add auth headers or not. Like so:
interface ApiService {
@Headers("isAuthorizable: false")
@GET("/app/Socks")
fun getShoes() : Call<Socks>
@GET("/app/shoes")
fun getShoes() : Call<Shoes>
@GET("/app/sandals")
fun getShoes() : Call<Sandals>
}
Check the header in the Interceptor
and add header if condition is satisfied. Like so:
public class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request()
val shouldAddAuthHeaders = original.headers["isAuthorizable"] != "false"
val requestBuilder = request
.newBuilder()
.method(request.method, request.body)
.removeHeader("isAuthorizable")
if (shouldAddAuthHeaders) {
requestBuilder.addHeader(AppConstant.HEADER_APP_TOKEN, AppConfig.apptoken)
.addHeader(AppConstant.HEADER_SECURITY_TOKEN, AppConfig.security_token)
}
return chain.proceed(requestBuilder.build())
}
}
Note: Using the same logic, one may specify requests for which auth headers are to be added instead of filtering out requests that do not require authentication. That is,
interface ApiService {
@GET("/app/Socks")
fun getShoes() : Call<Socks>
@Headers("isAuthorizable: true")
@GET("/app/shoes")
fun getShoes() : Call<Shoes>
@Headers("isAuthorizable: true")
@GET("/app/sandals")
fun getShoes() : Call<Sandals>
}
public class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request()
val shouldAddAuthHeaders = original.headers["isAuthorizable"] == "true"
val requestBuilder = request
.newBuilder()
.method(request.method, request.body)
.removeHeader("isAuthorizable")
if (shouldAddAuthHeaders) {
requestBuilder.addHeader(AppConstant.HEADER_APP_TOKEN, AppConfig.apptoken)
.addHeader(AppConstant.HEADER_SECURITY_TOKEN, AppConfig.security_token)
}
return chain.proceed(requestBuilder.build())
}
}
Answered By - Keyser Söze
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.