If I set content type via
Interceptor
fromokhttp
, an API Gateway will encode request body to base64.If I set content type via
Headers
annotation fromRetrofit
, the sever will not encode to base64.
I understand that need to decode from base64, I don't understand what's the difference on the client side?
// First case
object ApiFactory {
private val baseUrl = ""
fun create(): Api {
val client = OkHttpClient().newBuilder()
.addInterceptor(ContentTypeHeaderInterceptor())
.addInterceptor(HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.BODY
})
.build()
return createRetrofitApi(client, baseUrl)
}
inline fun <reified T : Any> createRetrofitApi(client: OkHttpClient, baseUrl: String, converterFactory: Converter.Factory? = null): T {
return Retrofit.Builder()
.addCallAdapterFactory(adapterFactory)
.addConverterFactory(converterFactory)
.baseUrl(baseUrl)
.client(client)
.build()
.create(T::class.java)
}
}
class ContentTypeHeaderInterceptor: Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
.newBuilder()
.header("Content-Type", "application/json")
.build()
return chain.proceed(request)
}
}
// Second case
interface Api {
@Headers("Content-Type: application/json")
@POST("signin")
suspend fun signIn(
@Body body: SignInRequest
): ResponseOkHttp<Response, Error>
}
// Logcat
// First case
--> POST
Content-Length: 1096
Content-Type: application/json
{"IdToken":"..."}
--> END POST (1096-byte body)
// Second case
--> POST
Content-Type: application/json
Content-Length: 1096
{"IdToken":"..."}
--> END POST (1096-byte body)