Upload binary file with retrofit 2 in Android : <p>I want to upload a binary file to the server in Android. I test Api method by postman:</p>

<p><a href="https://i.stack.imgur.com/fP58X.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fP58X.png" alt="enter image description here"></a></p>

<p>And it's OK, as you see there is another option which you can upload files as form data(key, value):</p>

<p><a href="https://i.stack.imgur.com/V0Lbt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/V0Lbt.png" alt="enter image description here"></a></p>

<p>Every tutorials (like <a href="https://futurestud.io/tutorials/retrofit-2-how-to-upload-files-to-server" rel="noreferrer">this one</a>)describe how to upload files as <code>multipart/form-data</code>:</p>

<pre><code> // create RequestBody instance from file
    RequestBody requestFile =
            RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // MultipartBody.Part is used to send also the actual file name
    MultipartBody.Part body =
            MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
</code></pre>

<p>I search a lot but couldn't find any way to upload file as binary with retrofit2.</p>

<p>There is just one Issue in <strong>retrofit</strong> repository which ask <strong><a href="https://github.com/square/retrofit/issues/1217" rel="noreferrer">How can I POST a image binary in retrofit 2.0 beta?</a></strong>. I use its solution :</p>

<p>API Service:</p>

<pre><code>@POST("trip/{tripId}/media/photos")
Call&lt;MediaPost&gt; postEventPhoto(
    @Path("eventId") int tripId,
    @Header("Authorization") String accessToken,
    @Query("direction") String direction,
    @Body RequestBody photo);
</code></pre>

<p>Caller:</p>

<pre><code>InputStream in = new FileInputStream(new File(media.getPath()));
byte[] buf;
buf = new byte[in.available()];
while (in.read(buf) != -1);
RequestBody requestBody = RequestBody
    .create(MediaType.parse("application/octet-stream"), buf);
Call&lt;MediaPost&gt; mediaPostCall = tripApiService.postTripPhoto(
    tripId,
    ((GlobalInfo) getApplicationContext()).getApiAccessToken(),
    direction,
    requestBody);
</code></pre>

<p>But I got this error:</p>

<pre><code>java.lang.IllegalArgumentException: @Body parameters cannot be used with form or multi-part encoding. 
</code></pre>

<p>What's wrong with my code? what should I do?</p>
