I am using put request to upload image and to the server using Okhttp, the request work fine with the postman, but through android it is giving me bad request i have implemented the method discussed here https://stackoverflow.com/a/23784452/9145387
Kindly help me out sorting this.... Thanks in Advance
public Boolean uploadFile(String serverURL, File file) {
OkHttpClient client = new OkHttpClient();
try {
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addFormDataPart("file", file.getName(),
RequestBody.create(MediaType.parse("image/jpg"), file))
.addFormDataPart(caseId+"_audio", caseId+"_audio",
RequestBody.create(MediaType.parse("audio/*"), Audio))
.build();
Request request = new Request.Builder()
.url(serverURL)
.put(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
e.printStackTrace();
// Handle the error
}
@Override
public void onResponse(Response response) throws IOException {
if (!response.isSuccessful()) {
response.toString();
// Handle the error
}
// Upload successful
}
});
}catch (Exception ex) {
ex.printStackTrace();
// Handle the error
}
return false;
}