最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - When API Gateway encode request body to base64? - Stack Overflow

programmeradmin2浏览0评论
  1. If I set content type via Interceptor from okhttp, an API Gateway will encode request body to base64.

  2. If I set content type via Headers annotation from Retrofit, 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)
发布评论

评论列表(0)

  1. 暂无评论