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 :
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 :
- https://android.jlelse.eu/working-with-retrofit-825d30348fe2
- https://inducesmile.com/android/android-upload-image-to-server-using-retrofit-2/
- image upload using multipart retrofit 2
- Upload image into server using retrofit
- https://www.simplifiedcoding.net/retrofit-upload-file-tutorial/
- https://www.journaldev.com/23738/android-multipart-image-upload-progress-retrofit-nodejs
- Image upload using retrofit
- How to Upload Image file in Retrofit 2
- Retrofit 2 Multipart image upload with data
- POST Multipart Form Data using Retrofit 2.0 including image
- Retrofit Uploading multiple images to a single key
And many more.....
But still not working. Please help