0

I have photo and that photo can have comments (optional) and I need to upload both photo and comment if there is some. I am using POST with Multipart for image uploading, but I do not know how to add photo comment to the API.

Here what I have for Retrofit interface:

@Multipart
@POST("/v3/work_orders/{work_order_id}/photo_attachments")
Single<Response<ResponseBody>> uploadPhoto(
  @Path("work_order_id") int workOrderId,
  @Part MultipartBody.Part imageBody);

Here is how I send data to that Retrofit interface:

@Override public Single<Response<ResponseBody>> uploadPhoto(
  int workOrderId, PhotoAttachmentRequest photoAttachmentRequest
) {
RequestBody requestFile =  RequestBody.create(MediaType.parse("image/png"),   photoAttachmentRequest.getAttachment());
MultipartBody.Part imageBody = MultipartBody.Part.createFormData(
    "photo_attachment[attachment]",
    photoAttachmentRequest.getAttachment().getName(),
    requestFile
);
return api.uploadPhoto(workOrderId, imageBody)
    .subscribeOn(Schedulers.io());
 }

How I can send also photo_attachement[comments] field?

Zookey
  • 2,637
  • 13
  • 46
  • 80
  • 1
    I think you can put comments in another MultipartBody. I had some problems with MultipartBody when it comes down to photo, so I asked backend if I can send photo as string (Base64) - it works without problem This issue should help: https://stackoverflow.com/questions/29680158/how-to-send-multipart-form-data-with-retrofit?rq=1 – Janusz Hain Aug 14 '19 at 06:33
  • 1
    Please check this link, it my help you https://stackoverflow.com/questions/39953457/how-to-upload-image-file-in-retrofit-2 – V.Lakade Aug 14 '19 at 06:38
  • If anyone wants to write answer based on those comments, I am glad to accept it. – Zookey Aug 14 '19 at 06:57

0 Answers0