Upload attachment using rest API

This one works. I would admit that it took me some time to get it right :smile:

curl https://demo.frappecloud.com/ -d "cmd=uploadfile&doctype=Lead&docname=LEAD-00149&filename=testfromapi.txt&filedata=aGVsbG93b3JsZAo%3D&from_form=1" -XPOST -b frappe.cookie

(file data is base64 + encoded)

4 Likes

Thanks! Works for me too…

Hii @Jens_Bohlin
Please Can you provide steps to upload file using rest API.

Thanks,
Ragh

Any suggestions?

I’ve been using this method and set Employee (because I’m using Employee Login) permission to the doc “r and w” and always got 403, even though with administrator, system engineer.

here’s request snapshot from my post man

POST / HTTP/1.1
Host: http://www.domain.com/
Content-Type: application/x-www-form-urlencoded
Accept: application/json
Authorization: Bearer
cache-control: no-cache
Postman-Token: 2a4d1d6b-390a-4295-ab0b-0c0ee76efc79
from_form=1doctype=Schedulesdocname=HR-EMP-00001-Jakarta-2019-02-25cmd=uploadfilefilename=test.jpgfiledata=iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAMAAAC%2FMqoPAAAAz1BMVEUAAADUBy%2FDDi7dAzDdAzDdAzDdAzDDDi7DDi7DDi7dAzDdAzDdAzDDDi7DDi7DDi7dAzDdAzDdAzDDDi7DDi7DDi7dAzDdAzDDDi7DDi7dAzDdAzDDDi7DDi7dAzDDDi7fEz3HHTvugZjhh5f97%2FL78PLqYn7%2F%2F%2F%2FaaHz74OX44eXmQmTSSmL3wMvww8vhI0rLLEjyobHppbHdAzDDDi7jMlfOO1XoUnHWWW%2F50Nj00tjscYvdd4nwkaTllqT0sL7stL7hRGPXBjDWBi%2FFDS4%2BJsiBAAAARXRSTlMAMDAwj9%2F%2F%2F9%2BPIHDPz3AgEGC%2Fv2AQUK%2BvUJ%2Fv75%2BAgP%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F9AQP%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F%2Fr6%2BTKVt1AAAH7ElEQ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dET0%2BwE5swmnbwOy5oGIvgr42FAZTp8HfK5oLaKf2XNm6sLpfmbPmtNINPvHhrIm9ML%2BuaJINXPOJK4J3afmrJHRW8aGzTfN6NvcWLNtHd362FQ2o8%2Btj1A6emz8duLUNaP7mfErjJ0D0DPDkTPQC%2BMjlI7%2ByJYziWtK96kta57K6Ctbzmya07e2rFnL6Ddsj01lc%2Frc9gh1N5LNlDNT15zuZ6asiXS7sDw2ZQS9sDxCXRPSW4acSRxB96kha9pC%2BmNDzmwY%2BtaQNU%2BE9NjwKeiSoc8NH5fuXDW97NctcwzdF4O6za%2Bavvrcnl3Y6A5DQRS%2BPzMzF5FUMO%2F139KSeJmONdLe08EIvsR29%2Be9Of3n1TkdyXt6kI1OvtPP00CbX12n3zZBNzw6Tr%2FMokTV0m36qo5SbTtO0%2FuHYAO8k79ulHfy143yTv66Ud6J183VO%2FG6uXonWDfeu1P56WdWN9478brhtZYlp6%2Ba4VTVKTW9X4dbi1OJ6ed1%2FDwD78Tr5uqdeN1cvROvm6t34nVz9U68bq7eidfN1Tvxurl6J0A3h6rxb0yfELrxLTo%2Fnd5ndDPwTj66AeOP359%2BYYfzDZffm74CWTfwTrxurt6J183VO%2FG6uXonXjdX78Tr5uqdeN1cvROvm6t3ctYNGN9%2BffoAGG7XcPdy%2Bt5aN%2BBxWvxjsat3InTz79E7PekWQPbeyV83qOG%2F%2F7PI%2FmhZlmVZlmVZlmVZlmXZPZmSvHpA7pEOAAAAAElFTkSuQmCC

I’ve successfully upload the image but the problem is with permission, the server said, not permitted , what method to pass this error while uploading attachment ?

I hope someone would tell what method they use to upload the attachment properly from android apps.

Thanks in advance.

1 Like

do you get some answer to solve this issue ? i have some problem too about post image, when im using Token Auth , its give me response 403 Forbidden , but if im using method/login its success to post image

Messed around for a bit and realised that the key names have changed. The attached works whether you call ‘/api/method/uploadfile’ or just ‘/’ with “cmd=uploadfile”.

4 Likes

Any updates on this?, I’m also getting error by using Token Auth. I’m integrating a small api for Node with Token Auth, anyone interested just drop me a line. I’ll be make it available as soon as uploading is working.

i create new api that return upload() function from erpnext directly and its work

1 Like

nice!, can you share it?.. i’m using npm package promise-request …
code just here: https://raw.githubusercontent.com/quad-tree/erpcloud_utils/master/api_erpnext_v11.js

Screenshot%20from%202019-07-15%2014-23-51

i just create a simple api like that and it’s work

3 Likes

how did you set your payload for this?

1 Like

thanks @MartinHBramwell

Did that get you past the problem you were having?

Worked for me too with image but when I’m working on video upload and when i encode a mp4 video to base64, and try to upload it using the API, it gives me error

{“exc”:“["Traceback (most recent call last):\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/handler.py\", line 139, in uploadfile\n ret.save()\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 273, in save\n return self._save(*args, **kwargs)\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 296, in _save\n self.insert()\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 223, in insert\n self.run_method(\"before_insert\")\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 797, in run_method\n out = Document.hook(fn)(self, *args, **kwargs)\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 1073, in composer\n return composed(self, method, *args, **kwargs)\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 1056, in runner\n add_to_return_value(self, fn(self, *args, **kwargs))\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/model/document.py\", line 791, in \n fn = lambda self, *args, **kwargs: getattr(self, method)(*args, **kwargs)\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/file/file.py\", line 55, in before_insert\n self.save_file(content=self.content, decode=self.decode)\n File \"/home/frappe/frappe-bench/apps/frappe/frappe/core/doctype/file/file.py\", line 455, in save_file\n self.content = base64.b64decode(self.content)\n File \"/home/frappe/frappe-bench/env/lib/python3.6/base64.py\", line 87, in b64decode\n return binascii.a2b_base64(s)\nbinascii.Error: Incorrect padding\n"]”}

Node.js

import fetch from 'node-fetch';

fetch('https://demo.frappecloud.com/', {
    method: 'POST',
    body: new URLSearchParams({
        'cmd': 'uploadfile',
        'doctype': '<<DocType>>',
        'docname': '<<DocName>>',
        'filename': '<<filename>>',
        'filedata': '<<filedata>>',
        'from_form': '1'
    })
});
3 Likes

Got any solution for permission error? I am facing the same issue when I am trying to upload a file where user don’t have any access token at the moment.

@arslanalamkhan there is an option in the system settings which allows a guest user to upload an attachment you can try it…

1 Like