How to define retrofit MultipartBody.Part Part Name in API Interfacce

241 views Asked by At

How can I define MultipartBody.Part Part Name in the API Interface.

So I want to define @Part image: MultipartBody.Part

To be like @Part("image") image: MultipartBody.Part

So I want some way that I can omit "image" from MultipartBody.Part.createFormData("image", image.name, requestBody)

But @Part("image") do not work with MulitipartBody.Part

    @Multipart
    @POST("someEndPoint")
    suspend fun uploadSometing(

        @Header("Authorization") token: String,

        @Part image: MultipartBody.Part

        ): Response
val requestBody = image.asRequestBody()

val imagePart = MultipartBody.Part.createFormData("image", image.name, requestBody)

1

There are 1 answers

0
itsmebil On

Have you try this ?

@Multipart
@POST("someEndPoint")
suspend fun uploadSometing(

    @Header("Authorization") token: String,

    @Part image: MultipartBody.Part

    ): Response

//code in your API services
fun editProfile(photo: File?) {
    viewModelScope.launch {
        try {
            val imageBody = photo?.let { 
                compressImage(it).toRequestBody("image") }
            val response = services.editImage(
                photo = imageBody
            )
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

so you define the "image" part name in the API interface