Skip to content Skip to sidebar Skip to footer

How To Send Array Of Objects In Retrofit Multipart Request

I want to send array objects with multipart data. I tried many ways but it is not working. My issue with the contributor parameter. Server says. contributor.0.id is required &

Solution 1:

Here is the only way worked for me.

First Created Hashmap and mapped my data on this way

val contributorsMap: HashMap<String, String> = HashMap()

    for((index, contributor) in contributorList.withIndex()){

        contributorsMap["contributors[${index}][id]"] = "${contributor.id}"
        contributorsMap["contributors[${index}][role]"] = contributor.role

    }

Then updated my function parameter to @PartMap instead

@Multipart@POST("project/create")
fun createProject(
    @Header("Authorization") token: String,
    @Part("title") title: String,
    @Partimg: MultipartBody.Part,
    @Part("release_date") releaseDate: String,
    @PartMapcontributors: HashMap<String, String>,
): Single<Response<String>>

Solution 2:

You can use @partMap

@PartMap() Map<String, List<Contributor>> contibutors

Solution 3:

Use @Field

@Field("contributors[]") contributors: MutableList<Contributor>

Post a Comment for "How To Send Array Of Objects In Retrofit Multipart Request"