File Upload via Rest API

If I upload a file using url following the REST API then it works. But if I upload a file from mobile device through uri than it gives me INTERNAL SERVER ERROR. What am I missing?

Please post API handler code and detailed response/error log

   **//Call upload function from async task**
    @Override
    protected String doInBackground(String... strings) {

            if (strings[0] != null) {
                try {
                    file1 = new File(strings[0]);
                 
                    RequestBody requestFile =
                            RequestBody.create(
                                    MediaType.parse(getContentResolver().getType(uri)),
                                    file1
                            );

                    // MultipartBody.Part is used to send also the actual file name
                    MultipartBody.Part body =
                            MultipartBody.Part.createFormData("file_url", file1.getName(), requestFile);

                    uploadFile(body);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    file2 = new File(strings[1]);
                   
                    // create RequestBody instance from file
                    RequestBody requestFile =
                            RequestBody.create(
                                    MediaType.parse(getContentResolver().getType(uri)),
                                    file2
                            );

                    // MultipartBody.Part is used to send also the actual file name
                    MultipartBody.Part body =
                            MultipartBody.Part.createFormData("file_url", file2.getName(), requestFile);
                    uploadFile(body);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;


    }

}

//Retrofit Call

private void uploadFile(MultipartBody.Part body){
        Log.v("HitMan",body.toString());
        String doctypeString = "Lead";
        String docnameString = name;
        RequestBody doctype =
                RequestBody.create(
                        okhttp3.MultipartBody.FORM, doctypeString);
 RequestBody docname =
                RequestBody.create(
                        okhttp3.MultipartBody.FORM, docnameString);
        Log.v("Body",body.toString());
        Log.v("Doctype",doctype.toString());
        Log.v("Docname",docname.toString());

    
        UserService uploadService = retrofit.create(UserService.class);

        Call<ResponseBody> call4 = uploadService.uploadFile(body,doctype,docname);
        call4.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if(response.isSuccessful()) {
                    progressBar.setVisibility(View.GONE);
                    Toast.makeText(LeadDetailActivity.this,"Uploaded Successfully",Toast.LENGTH_SHORT).show();
                }
                else{
                    progressBar.setVisibility(View.GONE);
                    Log.v("Custom Error :",response.message());
                    Toast.makeText(LeadDetailActivity.this,"Process Failed!",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                progressBar.setVisibility(View.GONE);
                Log.v("Create Response Error", t.toString());
            }
        });
    }




**//Interface**

    @Multipart
    @POST("api/method/upload_file")
    Call<ResponseBody> uploadFile(@Part MultipartBody.Part file_url,@Part("doctype") RequestBody doctype,@Part("docname") RequestBody docname);

//Response Error Log

V/URI: content://com.mi.android.globalFileexplorer.myprovider/external_files/temp.jpg
D/File Path :: /storage/emulated/0/temp.jpg
V/FA: Activity resumed, time: 5674872
V/FA: Connecting to remote service
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 1
V/HitMan: okhttp3.MultipartBody$Part@7a76c54
V/Body: okhttp3.MultipartBody$Part@7a76c54
V/Doctype: okhttp3.RequestBody$2@242e7fd
V/Docname: okhttp3.RequestBody$2@3da0cf2
V/Custom Error :: INTERNAL SERVER ERROR

Didn’t get what exactly is the problem. Can you post bench/frappe error log.

500 Error Code means Server Response Error, so I think if the end point is compatible with the post format as done using Multipart of okhttp3 client?

- - [21/Jun/2023 12:34:27] “POST /api/method/upload_file HTTP/1.1” 500 -
12:34:27 web.1 | INFO:werkzeug: - - [21/Jun/2023 12:34:27] “POST /api/method/upload_file HTTP/1.1” 500 -

I solved it , my assumption is correct, end point for uploading file via rest api is not that one. Thanks anyway.

1 Like