You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using Retrofit 2.11.0 and okhttp 4.12.0 in kotlin will end up in wrong sterilization of binary data as it will use toString() having the [] of the string representation in the body
val okHttpClient =OkHttpClient
.Builder()
.addInterceptor(httpLoggingInterceptor())
.build()
val retrofit =Retrofit
.Builder()
.baseUrl(config.url)
.client(okHttpClient)
// FIXME should not be needed .addConverterFactory(ByteArrayConverterFactory())
.build()
retrofit.create(ApiClient::class.java)
privatefunhttpLoggingInterceptor(): HttpLoggingInterceptor {
val logger =LogManager.getLogger(ApiClient::class.java)
val logging =HttpLoggingInterceptor { logger.info(it) }
logging.setLevel(HttpLoggingInterceptor.Level.BODY)
logging.redactHeader("Authorization")
logging.redactHeader("x-api-key")
return logging
}
interfaceApiClient {
@POST("//binary")
@Headers("Content-Type: application/octet-stream")
funbinary(
): Call<ResponseBody>
// the workaroundclassByteArrayConverterFactory : Converter.Factory() {
overridefunrequestBodyConverter(
type:Type,
parameterAnnotations:Array<Annotation>,
methodAnnotations:Array<Annotation>,
retrofit:Retrofit,
): Converter<ByteArray, RequestBody>? {
if (type ==ByteArray::class.java) {
returnConverter { value ->RequestBody.create(
"application/octet-stream".toMediaTypeOrNull(),
value,
)
}
}
returnnull
}
overridefunresponseBodyConverter(
type:Type,
annotations:Array<Annotation>,
retrofit:Retrofit,
): Converter<ResponseBody, ByteArray>? {
if (type ==ByteArray::class.java) {
returnConverter { value ->
value.use { it.bytes() }
}
}
returnnull
}
}
The text was updated successfully, but these errors were encountered:
rowi1de
changed the title
retrofit / okhttp wrong seriliazation of ByteArray (toString()) in kotlin
retrofit / okhttp wrong serialization of ByteArray (toString()) in kotlin
Jan 6, 2025
Using Retrofit 2.11.0 and okhttp 4.12.0 in kotlin will end up in wrong sterilization of binary data as it will use toString() having the
[]
of the string representation in the bodyThe text was updated successfully, but these errors were encountered: