Passing arrays in multipart POST using Okhttp
<p>I'm building an app that requires an API call that has array in its POST body request. I'm using OkHttp 2.6 to request for API.</p>

<p>The request in Postman looks like this:</p>

<p><a href="https://i.stack.imgur.com/YhYoJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YhYoJ.jpg" alt="postman-ss"></a></p>

<p>I have tried several ways of writing <code>RequestBody</code> in order to achieve this,</p>

<ol>
<li><p>First method</p>

<pre><code>RequestBody requestBody = new MultipartBuilder()
        .type(MultipartBuilder.FORM)
        .addFormDataPart("app_token", MY_TOKEN)
        .addFormDataPart("user_id", 377)
        .addFormDataPart("group_id", String.valueOf(groupId))
        .addFormDataPart("key_id", deals.toString())
        .addFormDataPart("img", filename + ".jpg", fileBody)
        .build();
</code></pre></li>
<li><p>Second method</p>

<pre><code>RequestBody requestBody = new MultipartBuilder()
        .type(MultipartBuilder.FORM)
        .addFormDataPart("app_token", MY_TOKEN)
        .addFormDataPart("user_id", 377)
        .addFormDataPart("group_id", String.valueOf(groupId))
        .addFormDataPart("key_id[0]", "33")
        .addFormDataPart("key_id[1]", "34")
        .addFormDataPart("img[0]", filename + ".jpg", fileBody)
        .build();
</code></pre></li>
<li><p>Third method, which I found <a href="https://stackoverflow.com/questions/37049054/how-to-send-string-array-data-with-okhttp-multipartbuilder-data">here</a>.</p>

<pre><code>MediaType json = MediaType.parse("application/json; charset=utf-8");
Map&lt;String, Integer&gt; params = new HashMap&lt;&gt;();
for (int i = 0; i &lt; deals.size(); i++) {
    params.put("key_id["+i+"]", Integer.valueOf(deals.get(i).getUid()));
}
JSONObject parameter = new JSONObject(params);
RequestBody theBody = RequestBody.create(json, parameter.toString());


RequestBody requestBodyyy = new MultipartBuilder()
        .type(MultipartBuilder.FORM)
        .addFormDataPart("app_token", MY_TOKEN)
        .addFormDataPart("user_id", 377)
        .addFormDataPart("group_id", String.valueOf(groupId))
        .addPart(theBody)
        .addFormDataPart("img[0]", filename + ".jpg", fileBody)
        .build();
</code></pre></li>
</ol>

<p>But, none of them are working.</p>

<p>What is the correct way of doing this? I also need the array size to be dynamic as I'm not passing a static one.</p>