Community
    • Categories
    • Recent
    • Popular
    • Users
    • Search
    • Register
    • Login

    Upload file using API

    Scheduled Pinned Locked Moved Development
    3 Posts 2 Posters 658 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • F Offline
      fa__
      last edited by

      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:
      ca2b4ccd-039e-4ad7-b6ec-89dc13cc6f20-imagen.png

      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?

      Michael HuhnM 1 Reply Last reply Reply Quote 0
      • Michael HuhnM Offline
        Michael Huhn @fa__
        last edited by

        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
        }
        
        F 1 Reply Last reply Reply Quote 0
        • F Offline
          fa__ @Michael Huhn
          last edited by

          @michael-overkamp
          It works fine now.

          Thank you!

          1 Reply Last reply Reply Quote 0
          • L leno2206 referenced this topic on
          • First post
            Last post