vuhung3990
vuhung3990

Reputation: 6809

Upload file with application/x-www-form-urlencoded use retrofit

how to config retrofit work like image below

enter image description here

this is my code:

    // interface ImageUploadService

    @Multipart
    @POST("/api=upl_img_version_2&token={token}&img_cat=3&sum={sum}")
    Observable<ServerResponse> uploadAvatar(@Path("token") String token, @Path("sum") String sum,  @Part MultipartBody.Part file);
    ===========================================

    //File creating from selected URL
    File file = new File(path);

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

    // body part send to server
    MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);

    view.showLoadingDialog();
    imageUploadService.uploadAvatar(token, sum, body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<ServerResponse>() {
                        @Override
                        public void onCompleted() {
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onNext(ServerResponse serverResponse) {
                            Log.d("aaaaa", serverResponse.toString());
                        }
                    })

then use burp suite to capture request and it's different with ios (working), i don't have any idea or keyword about that. Thanks

Upvotes: 2

Views: 1713

Answers (1)

vuhung3990
vuhung3990

Reputation: 6809

i've just found a solution and want to help someone face same issue instead of delete this question.

here is working code:

interface ImageUploadService {
   //@Multipart
  @POST("/api=upl_img_version_2&token={token}&img_cat=3&sum={sum}")
  Observable<ServerResponse> uploadAvatar(@Path("token") String token, @Path("sum") String sum,  @Body RequestBody file); // @Part => @Body
}

///////////////////////////////
File file = new File(path);

// MediaType.parse("multipart/form-data")
RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
imageUploadService.uploadAvatar(token, sum, requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<ServerResponse>() {
                        @Override
                        public void onCompleted() {
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                            view.hideLoadingDialog();
                        }

                        @Override
                        public void onNext(ServerResponse serverResponse) {
                            Log.d("aaaaa", serverResponse.toString());
                        }
                    })

Upvotes: 1

Related Questions