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
);