admin管理员组文章数量:1220269
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)
本文标签: androidWhen API Gateway encode request body to base64Stack Overflow
版权声明:本文标题:android - When API Gateway encode request body to base64? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739342936a2159022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论