0

I am trying to upload an image to the Webserver using retrofit.

The API has been developed using Dot Net. The API is working fine on postman but shows error in android.

URL: https://m13tp89pn9.execute-api.us-west-2.amazonaws.com/Prod/api/user/UploadImag/userid=67

Authorization = Bearer token

This is my interface

@Headers("Content-Type: multipart/form-data")
@Multipart
@POST("user/UploadImage")
Call<GeneralPOJO> uploadProfileImage(@Query ("userid") int uid,
                                     @Part MultipartBody.Part image, @Header("Authorization")String header);

This is my Code

    final String path = getPathFromURI(selectedImageUri);
                    if (path != null) {
                        File file = new File(path);

   RequestBody mFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                        MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getAbsolutePath(), mFile);
                        RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());


new RestCaller(ProfileActivity.this, MainApplication.getRestClient().getApiServices().
                                    uploadProfileImage(AppSession.getInt(Constants.LOGIN_UDID), fileToUpload, AppSession.get(Constants.LOGIN_TOKEN)), Constants.REQUEST_CODE_PROFILE_PICTURE);

Please help me in uploading the image to the server successfully. Please correct my code and tell me what change is required. Thanks

Juan Cruz Soler
  • 8,172
  • 5
  • 41
  • 44
Wasif
  • 61
  • 10

1 Answers1

0
// Interface

@Multipart
@POST("user/UploadImage/{userid}")
Call<GeneralPOJO> uploadProfileImage(@Path ("userid") int uid,
                                     @Part MultipartBody.Part image, @Header("Authorization")String header);
  • 1
    It would be great if you provide explanation why this is the preferred solution as we want to educate, not just serve ready made code. – Hardik Upadhyay Feb 17 '20 at 06:36