Upload file using API
-
Hello,
We are trying to automate our operation with i-doit, including file handling.
I have managed to download files using the API but not to upload them. Plain text files are enough for our user case.Download
To download, I have checked that it can be done reading categories 'Files > File versions' and 'Files >Current file', that return the file base64 encoded. File is returned in either "file_physical" or "file_content" attribute with the following format, where "title" includes the file content:"file_content": { "file_name": "2943__1621850165__test-file.txt", "title": "<![CDATA[ZmlsZV9waHlzaWNhbAkNCmZpbGVfY29udGVudAkNCmZpbGVfdGl0bGUJDQpyZXZpc2lvbgkNCnVwbG9hZF9kYXRlCQ0KdmVyc2lvbl9kZXNjcmlwdGlvbgkNCm1kNV9oYXNoCQ0KdXBsb2FkZWRfYnk=]]>" }
Upload
In the web interface, new files are uploaded when creating a new file version. However, I have not been able to do the same using the API. I have tried including my file in the "file_content" attribute, both in plain text and base64 encoded. My request:{ "version": "2.0", "method": "cmdb.category.create", "params": { "objID": 2943, "category": "C__CATS__FILE_VERSIONS", "apikey": "c1ia5q", "language": "en", "data": { "file_title": "file_title", "version_description": "New version", "file_physical": "test.txt", "file_content": "<![CDATA[ZmlsZV9waHlzaWNhbAkNCmZpbGVfY29udGVudAkNCmZpbGVfdGl0bGUJDQpyZXZpc2lvbgkNCnVwbG9hZF9kYXRlCQ0KdmVyc2lvbl9kZXNjcmlwdGlvbgkNCm1kNV9oYXNoCQ0KdXBsb2FkZWRfYnk=]]>" } }, "id": 1 }
Then, a new file version is created but the file is not included:
The error returned is the following:
"error": { "code": -32099, "message": "i-doit system error: Signal error (mod.cmdb.afterCategoryEntrySave) : Filesystem error: Filename can't be empty", "data": null }
I have also tried using the same schema included in the API response, including the file in "file_content":{"title"}. It also failed, but this time the file version is not created and
500 Internal Server Error is returned
.Is it possible to upload files using the API? How is the file sent?
-
Hello @fa__,
you did nealy all right, except of the "file_content" where you only add the base64 string.
Try this:{ "version": "2.0", "method": "cmdb.category.create", "params": { "objID": 2943, "category": "C__CATS__FILE_VERSIONS", "apikey": "c1ia5q", "language": "en", "data": { "file_title": "file_title", "version_description": "New version", "file_physical": "test.txt", "file_content": "ZmlsZV9waHlzaWNhbAkNCmZpbGVfY29udGVudAkNCmZpbGVfdGl0bGUJDQpyZXZpc2lvbgkNCnVwbG9hZF9kYXRlCQ0KdmVyc2lvbl9kZXNjcmlwdGlvbgkNCm1kNV9oYXNoCQ0KdXBsb2FkZWRfYnk=" } }, "id": 1 }
-
@michael-overkamp
It works fine now.Thank you!
-