demo_Ashif
demo_Ashif

Reputation: 185

Retrofit 2 Multipart file upload Error

Trying to upload profile picture through api call using Retrofit 2 and rxjava. Response is always gives me error which is 422 Unprocessable Entity. Giving my code and need suggestion to overcome this.

@Multipart
@POST("api/update-profile-picture")
Observable<ProfilePicture> updateProfilePicture(
        @Header("Authorization") String accessToken,
        @Part("profile_picture") RequestBody profile_picture
);


// calling presenter method to update profile picture
public void updateProfilePictureImage(File file){
    getMvpView().showProgress();

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

    oyeBuddyService.updateProfilePicture("Bearer" + " " + mPrefs.getOyeUserAccessToken(), reqFile)
            .subscribeOn(mNewThread)
            .observeOn(mMainThread)
            .subscribe(new Observer<ProfilePicture>() {
                @Override
                public void onCompleted() {
                    getMvpView().hideProgress();
                }

                @Override
                public void onError(Throwable e) {
                    getMvpView().hideProgress();
                    Log.e("error: ",e.getMessage());
                }

                @Override
                public void onNext(ProfilePicture profilePicture) {
                    Log.e("response: ", profilePicture.toString());
                    getMvpView().onProfilePictureUpdated(profilePicture.profile_picture_url);
                }
            });
}

Response --->

RetrofitModule: Received response for http://174.138.64.95/api/update-profile-picture in 4540.6ms
                                                                                 Date: Wed, 04 Oct 2017 10:30:53 GMT
                                                                                 Server: Apache/2.4.18 (Ubuntu)
                                                                                 Vary: Authorization
                                                                                 Cache-Control: no-cache, private
                                                                                 X-RateLimit-Limit: 60
                                                                                 X-RateLimit-Remaining: 59
                                                                                 Content-Length: 99
                                                                                 Keep-Alive: timeout=5, max=99
                                                                                 Connection: Keep-Alive
                                                                                 Content-Type: application/json

E/error:: HTTP 422 Unprocessable Entity

Upvotes: 1

Views: 961

Answers (2)

Jenish Khanpara
Jenish Khanpara

Reputation: 76

You need to add header like:

Accept = */*

For Example:

httpClient.addInterceptor(new Interceptor() {  
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request original = chain.request();

        Request request = original.newBuilder()
            .header("Accept", "*/*")
            .method(original.method(), original.body())
            .build();

        return chain.proceed(request);
    }
}

Upvotes: 0

etan
etan

Reputation: 593

You should pass @Part MultipartBody.Part profile_picture instead of RequestBody to Retrofit interface.

Upvotes: 1

Related Questions