Reputation: 2587
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 :
And many more.....
But still not working. Please help
Upvotes: 1
Views: 1387
Reputation: 1997
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)
Upvotes: 1
Reputation: 66
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;
}
}
Upvotes: 2
Reputation: 1079
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);
Upvotes: 2