2

I have a rather complex POST I am trying to make with Retrofit2.

    @Multipart
    @FormUrlEncoded
    @POST("post")
    Call<JSONObject> createPost(
        @Field("name") String name,
        @Part("media_1") MultipartBody.Part mediaOne,
        @Part("media_2") MultipartBody.Part mediaTwo,
        @Part("media_3") MultipartBody.Part mediaThree,
        @Part("media_4") MultipartBody.Part mediaFour,
        @Part("media_5") MultipartBody.Part mediaFive,
        @Header("secret_key") int secretKey
    );

It contains various aspects such as a parameter called "name" which I would normally think to post with an @Field and with @FormUrlEncoded. I understand that I cannot have @Multipart and @FormUrlEncoded at the same time so I think I would want to remove @FormUrlEncoded from the above code example and replace the @Field with @Part. Is this correct?

Then, I get the exception "@Part parameters using the MultipartBody.Part must not include a part name in the annotation". But it's not like I can just remove @Part("media_1") etc. because those part names make sure the media file is uploaded to the correct location. What is the solution here?

This is the most complex Retrofit2 call I've ever made. Thanks for taking the time to review my question.

Here's where I'm using the Retrofit2 call just in case it's helpful to have it as context:

 file0 = FileUtils.getFile(filePath);
 requestFile0 = RequestBody.create(MediaType.parse("multipart/form-data"), file0);
 body0 = MultipartBody.Part.createFormData("image0", file0.getName(), requestFile0);

 file1 = FileUtils.getFile(constructedLog.getLogImageLocations().get(1));
 requestFile1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
 body1 = MultipartBody.Part.createFormData("logImage1", file1.getName(), requestFile1);

 //etc for file2, file3, file4

 Call<JSONObject> call = apiService.getApi().createPost(
            getName(),
            body0,
            body1,
            body2,
            body3,
            body4,
            secretKey
            );
Boxwood
  • 71
  • 1
  • 2
  • 8

1 Answers1

3

The code for the API call:

@Multipart
@POST("post")
Call<JSONObject> createPost(
    @Part("name") RequestBody name,
    @Part MultipartBody.Part mediaOne,
    @Part MultipartBody.Part mediaTwo,
    @Part MultipartBody.Part mediaThree,
    @Part MultipartBody.Part mediaFour,
    @Part MultipartBody.Part mediaFive,
    @Header("secret_key") int secretKey
);

File file = new File(yourpathhere);
body0 = MultipartBody.Part.createFormData("media_1", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));

RequestBody name = RequestBody.create(MediaType.parse("text/plain"), yourString);


Call<JSONObject> call = apiService.getApi().createPost(
        name,
        body0,
        body1,
        body2,
        body3,
        body4,
        secretKey
        );

Essentially, the issue was that I was declaring the name in both createFormData() and @Part. You can only do one or the other.

I also changed the first param to a RequestBody. If you were to just try to use a String, at least in my case, the API would think there was literally supposed to be a double quote around it.

Link to that solution: Retrofit 2.0-beta-2 is adding literal quotes to MultiPart values

Boxwood
  • 71
  • 1
  • 2
  • 8