在特定改造请求 Android Studio 中删除 header
Remove header in a specific retrofit request Android Studio
有没有办法在设置这种拦截器后删除 特定的 header :
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)
这是我的 Retrofit 实例:
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)
}
}
这是我的 API 服务:
interface ApiService {
@GET("/app/shoes")
fun getShoes() : Call<Shoes>
}
感谢您的帮助:)
反其道而行之。添加一个 header 到 API 调用,指示是否添加 auth headers 或不。像这样:
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>
}
勾选Interceptor
中的header,满足则添加header。像这样:
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())
}
}
注意: 使用相同的逻辑,可以指定要为其添加身份验证 header 的请求,而不是过滤掉不需要身份验证的请求。也就是说,
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())
}
}
有没有办法在设置这种拦截器后删除 特定的 header :
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)
这是我的 Retrofit 实例:
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)
}
}
这是我的 API 服务:
interface ApiService {
@GET("/app/shoes")
fun getShoes() : Call<Shoes>
}
感谢您的帮助:)
反其道而行之。添加一个 header 到 API 调用,指示是否添加 auth headers 或不。像这样:
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>
}
勾选Interceptor
中的header,满足则添加header。像这样:
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())
}
}
注意: 使用相同的逻辑,可以指定要为其添加身份验证 header 的请求,而不是过滤掉不需要身份验证的请求。也就是说,
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())
}
}