1
votes

I am uploading an image with retrofit multipart api. i am getting success in postman but getting below error in code :

Response{protocol=http/1.1, code=422, message=Unprocessable Entity, url=http://upload-snack.13.251.251.232.nip.io/upload}

Postman :

enter image description here

Retrofit code :

Request :

 @Multipart
    @POST("upload")
    Call<ResponseBody> uploadImage(@Part MultipartBody.Part image);

Interface :

 public static Retrofit getRetrofitClient(Context context, String baseURL) {
        if (retrofit == null) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseURL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

Activity class code :

 private void uploadToServer(String filePath) {
        Retrofit retrofit = ServiceGenerator.getRetrofitClient(this, "http://upload-snack.13.251.251.232.nip.io/");
        Api uploadAPIs = retrofit.create(Api.class);
        //Create a file object using file path
        File file = new File(filePath);
        // Create a request body with file and image media type
        RequestBody fileReqBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        // Create MultipartBody.Part using file request-body,file name and part name
        MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), fileReqBody);
        //Create request body with text description and text media type
       // RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "image-type");
        //
        Call call = uploadAPIs.uploadImage(part);
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                Log.e("response", response.toString());
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                Log.e("failure", "failure");
            }
        });
    }

I have checked below tutorials/answers but nothing works :

And many more.....

But still not working. Please help

3
I am also facing the same issue, did you find the solution for this? Please let me know if there are any solutions.Akshay kumar
@Akshay need to change media type to image/png instead of multipart/form-data. That will work.Vidhi Dave
@VishvaDave Thankyou very much, it's working now.Akshay kumar

3 Answers

2
votes

Change Media type image/* instance of multipart/form-data. i think this will help you.

 File file = new File(filePath);
    // Create a request body with file and image media type
    RequestBody fileReqBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    // Create MultipartBody.Part using file request-body,file name and part name
    MultipartBody.Part part = MultipartBody.Part.createFormData("image", file.getName(), fileReqBody);
2
votes

Change your Retrofit interface methode to this.

public class ServiceGenerator{

    private static final String BASE_URL = "base_url";


    public static Retrofit getRetrofitClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        return retrofit;
    }
}
1
votes

I faced the same issue in Kotlin but thanks to @VishvaDave. So I did it in this way

val file = File(filePath)
val fileReqBody = RequestBody.create(MediaType.parse("image/png"), file)
val part = MultipartBody.Part.createFormData("image", file.name, fileReqBody)