File upload using Rest API not working

Hello,

I am trying to attach some file to any doctype as per steps given in the link Upload Files with Rest API
and I gets success response and I can see that file in my sites/my_site/public/files folder but I can’t see it in the file list in erpnext.

it is also not seen linked to the doctype I have attached the file to.

@Harsh_Mehta,

Check this - Upload attachment using rest API

Also if it does not work share your API as well as a response what you get.

Hello,

Below is my request Url data :

http://192.168.123.72:8000/api/method/run_custom_method?cmd=uploadfile&doctype=Item&docname=Test%20Item%20%201&filename=api.txt&filedata=SGVsbG8gV29ybGQ=&from_form=1

Below is my responce data :

{“message”:{“comment”:{“ref_doctype”:“Item”,“modified_by”:“Administrator”,“name”:“8f3cf8f01b”,“parent”:null,“creation”:“2018-01-16 21:33:11.137426”,“modified”:“2018-01-16 21:33:11.137426”,“doctype”:“Version”,“idx”:0,“parenttype”:null,“docname”:“Test Item 1”,“owner”:“Administrator”,“docstatus”:0,“data”:"{\n “comment”: “added api2e3fe5.txt”, \n “comment_type”: “Attachment”\n}",“parentfield”:null},“file_name”:“api2e3fe5.txt”,“file_url”:"/files/api2e3fe5.txt",“name”:“7cf844481c”,“is_private”:0}}

So, here I can see that attachment is uploaded into coment section for that item but when I see that Item in erp it nothing happened. Also i have seen into communication but also not there. After that i go to sites files folder and my attachement file is in public folder.

Reply if possible.

Check into File Master.

It’s not there.

Open it. If the file is available it’s get downloaded as it is the text file.

Check

http://192.168.123.72:8000/files/api2e3fe5.txt

I have already checked. It’s open in browser and i get my contents of that file. I have seen File doctype and the fields in that doctype and response after file attached using rest API both are different.

I didn’t understand your concern. If the file is uploading using API.

Mention diferences here. If you want to add extra data in file you can handle this using custom API/method

Hello,

Always Doctype field data will be return into response of rest api but here when you checked response data which are below are totally different from File doctype.

Request : http://192.168.123.72:8000/api/method/run_custom_method?cmd=uploadfile&doctype=Item&docname=Test%20Item%20%201&filename=api.txt&filedata=SGVsbG8gV29ybGQ=&from_form=1

Resposne : {“message”:{“comment”:{“ref_doctype”:“Item”,“modified_by”:“Administrator”,“name”:“8f3cf8f01b”,“parent”:null,“creation”:“2018-01-16 21:33:11.137426”,“modified”:“2018-01-16 21:33:11.137426”,“doctype”:“Version”,“idx”:0,“parenttype”:null,“docname”:“Test Item 1”,“owner”:“Administrator”,“docstatus”:0,“data”:"{\n “comment”: “added api2e3fe5.txt”, \n “comment_type”: “Attachment”\n}",“parentfield”:null},“file_name”:“api2e3fe5.txt”,“file_url”:"/files/api2e3fe5.txt",“name”:“7cf844481c”,“is_private”:0}}

Below are the response when you get data from particular file name. Checked both response are different.

Request : http://192.168.123.72:8000/api/resource/File/b458a51aae

Response : {“data”:{“attached_to_name”:“UPS”,“file_name”:“test1.txt”,“creation”:“2018-01-15 15:47:07.700383",“doctype”:“File”,“lft”:131,“is_attachments_folder”:0,“is_folder”:0,“file_size”:613,“owner”:"harsh.mehta@augustinfotechteam.com",“content_hash”:“e0559d0ae32c0f58035b6c1e735cbab7”,“modified_by”:"harsh.mehta@augustinfotechteam.com”,“is_home_folder”:0,“docstatus”:0,“folder”:“Home/Attachments”,“attached_to_doctype”:“Item”,“rgt”:132,“is_private”:1,“name”:“b458a51aae”,“idx”:0,“file_url”:"/private/files/test1.txt",“modified”:“2018-01-15 15:47:07.720506”,“old_parent”:“Home/Attachments”}}

The above response are perfect. So when we use rest api to insert file into ERP above result should be return but it not seems like that.

1 Like