How to replace deprecated okhttp.RequestBody.create()

You can use the Kotlin extensions as well.

val requestImageFile = imageFile.asRequestBody("image/*".toMediaTypeOrNull())

Here is how to do it easily with kotlin extension functions from okhttp like: toRequestBody():

change from :

val requestImageFile = RequestBody.create(
                            MediaType.parse("image/*"),
                            imageFile
                    );

to this:

val requestImageFile = imageFile.toRequestBody(MediaType.parse("image/*"))

' more info here: https://square.github.io/okhttp/upgrading_to_okhttp_4/


You can change from:

RequestBody.create(MediaType.parse("image/*"), imageFile);

to:

RequestBody.Companion.create(imageFile, MediaType.parse("image/*"))

Just swap the parameters from

RequestBody.create(MediaType.parse("image/*"), imageFile);

to

RequestBody.create(imageFile, MediaType.parse("image/*"));