Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Igor Touguinhó
    [Knowledge Advanced] Adding attachment to content
    Topic posted October 30, 2019 by Igor TouguinhóBlue Ribbon: 750+ Points, tagged REST 
    14 Views, 2 Comments
    Title:
    [Knowledge Advanced] Adding attachment to content
    Summary:
    How can I add attachments to content using rest api
    Content:

    I'm trying to develop an application that creates contents on my KA. First, I was only creating and publishing contents with text fields, but now I want to add attachments to the contents, even using UPDATE or CREATE service.

    I did not found on documentation how can I do this. 

    Need some help to create this integration, anybody can help me ?

    Version:
    KA 19C

    Comment

     

    • Thom Blake

      Igor,

      We do have sample code in the form of a Java application that does this. It's right here in our knowledge base: Answer 8937 - Knowledge Advanced REST API content record creation example. The documentation page for the POST content endpoint links to it.

      Although I'm not a developer and I don't remember everything from 3 years ago, I was able to create the request with MIME type multipart/form-data. The gotchas I encountered in 2016:

      1. Each part should have a Content-Disposition and Content-Type specified. The content-type should be the MIME type of the attached file, or the content object (application/json or application/xml) The first example is for the content object:

      Content-Disposition: form-data; name="contentBO"
      Content-Type: application/json

      For the attachments, all of them should be named filesToUpload in the Content-Disposition. A couple examples:

      Content-Disposition: form-data; name="filesToUpload"; filename="test.pdf"
      Content-Type: application/pdf

      Content-Disposition: form-data; name="filesToUpload"; filename="1.png"
      Content-Type: image/png

      2. In the content XML, you should have the attachments like below. I think the description is optional, and this example uses a test pdf and two png files that I don't still have. The reference key for my repeatable attachment node was ATTACHMENTS and the reference key for the files themselves was ATTACHMENT, so yours could look different (and of course the record IDs and refkeys for the content types, etc.). This content object was sufficient for me to create the content. I believe the size is required.

      {
        "locale": { "recordId": "en_US" },
        "isForEdit": true,
        "contentType": {
          "recordId": "00245018fece66015482c48a4d007faf",
          "referenceKey": "RESOURCES",
          "name": "Resources"
        },
        "published": false,
        "checkedOut": false,
        "displayPosition": "HISTORICAL_USEFULNESS",
        "xml": "<RESOURCES><TITLE><![CDATA[Here is a test article!]]></TITLE><CONTENT><![CDATA[Some stuff is in the content area.]]></CONTENT><ATTACHMENTS><FILE_DESCRIPTION><![CDATA[A PNG that says 1]]></FILE_DESCRIPTION><ATTACHMENT SIZE=\"266\">1.png</ATTACHMENT></ATTACHMENTS><ATTACHMENTS><FILE_DESCRIPTION><![CDATA[A PNG that says 2]]></FILE_DESCRIPTION><ATTACHMENT SIZE=\"446\">2.png</ATTACHMENT></ATTACHMENTS><ATTACHMENTS><FILE_DESCRIPTION><![CDATA[A PDF that says test]]></FILE_DESCRIPTION><ATTACHMENT SIZE=\"9005\">test.pdf</ATTACHMENT></ATTACHMENTS></RESOURCES>",
        "categories": null,
        "userGroups": null,
        "views": [
          {
            "recordId": "00246060cc48b40151a7479f37007d99",
            "referenceKey": "INTENTIONALLY_INVISIBLE",
            "name": "intentionally invisible"
          }
        ]
      }
      

      Ultimately, to get a good handle on what the content object should look like, I recommend creating some content in Authoring with attachments and then looking at it via a GET request in the API with mode=FULL&isForEdit=TRUE specified as query parameters.

      • Igor Touguinhó

        Hi Thom,

        I saw this link but it was broken until yesterday, I opened a SR to update this link smiley

        I'll look at this example right now.