0

I am trying to upload an image on "https://api.cloudsightapi.com/image_requests" but after request call i am getting all the fields null into response .i.e status,name,token etc. I am using retrofit 2.0.1 for making request call.

Code:

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

MultipartBody.Part body =
MultipartBody.Part.createFormData("image_request[image]", file.getName(), requestFile);

String descriptionString = "en-US";
RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

**API CALL:**
@Multipart
    @POST("https://api.cloudsightapi.com/image_requests")
    Call<FileUploadResponse> uploadPhoto(
            @Header("Authorization") String authorisation,
            @Part("image_request[locale]") RequestBody description,
            @Part MultipartBody.Part file);

I am getting all the fields into response.body null. Please help.

pankaj
  • 1
  • Getting following When try to send MediaType.parse("multipart/form-data") : {"error":{"image":["at least one of image or remote_image_url must be set","You are not allowed to upload multipart/form-data files"]}} When try to send MediaType.parse("image/*") : {"error":{"image":["at least one of image or remote_image_url must be set","You are not allowed to upload image/* files"]}} – pankaj May 17 '17 at 11:05
  • CloudSight replied me as following: *body's* content type should be the mime type of the image file (image/jpg, image/png, etc Instead of MultipartBody.Part ??. Anybody knows how to convert it? I am using Retrofit 2.0.1 . Please Help. – pankaj May 19 '17 at 10:05

2 Answers2

0

Upload image file like this:

File file = // initialize file here

MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));

Call<MyResponse> call = api.uploadPhoto(filePart);//u can send other parameters along with the filePart depending upon ur method signature
Avinash Roy
  • 953
  • 1
  • 8
  • 25
  • RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file); MultipartBody.Part body = MultipartBody.Part.createFormData("image_request[image]", file.getName(), requestFile); – pankaj May 17 '17 at 10:48
0

CloudSight stopped accepting MultipartBody.Part So we have to send RequestBody i.e file with image name.

I have solved the issue with following steps:

RequestBody requestFile = RequestBody.create(MediaType.parse("image*/"), file);

String descriptionString = "en-US";

RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

apiInterface.uploadPhoto("CloudSight key",description, requestFile);


In retrofit api call:

@Multipart
    @POST("https://api.cloudsightapi.com/image_requests")
    Call<FileUploadResponse> uploadPhoto(
            @Header("Authorization") String authorisation,
            @Part("image_request[locale]") RequestBody description,
            @Part("image_request[image]\"; filename=\"file.jpg\" " ) RequestBody file);

Hope This helps. Thanks.

pankaj
  • 1