Friday, 24 January 2020

Uploading a file in an array of object using Retrofit 2

I use Retrofit2 and I need to upload various files using file in an array of objects Media like this :

{
   "state" = "done",
   "medias" = [
     {
      "file" = THE_FILE1
     },
     {
      "file" = THE_FILE2
     },
     {
      "file" = THE_FILE3
     }
   ]
}

This is the function of my Interface :

@Multipart
@POST("api/exercice/{id}")
fun submitExercice(
    @Path("id") id: Int,
    @Header("Authorization") token: String,
    @Body data: AnswerExercice
): Call<Void>

And this is my object Media :

    data class AnswerExercice(
    val state: String = "done",
    val medias: List<Media>
) : Serializable {
    data class Media(
        @Part val file: MultipartBody.Part
    ) : Serializable
}

But I have this error :

@Body parameters cannot be used with form or multi-part encoding. (parameter #3)

What am i not doing well?



from Uploading a file in an array of object using Retrofit 2

No comments:

Post a Comment