Forum Discussion

msommer's avatar
msommer
Contributor
9 years ago

Create message with a binaray attachment (zip archive) via REST API v2 fails

Creating a message with an attachment (zip archive) works with REST API v1 as explained in this thread by HaidongG: https://community.lithium.com/t5/Developers-Discussion/We-can-not-find-rest-API-for-attachment/m-p/127081

 

I have to use oauth2 authentication so I'm restricted to REST API 2. However creating a message with an attachment results in the following response

 

{u'error': {u'code': 100, u'message': u''}, u'status': u'error'}

Sending the request without an attachment or a text only attachment successfully creates the message.

 

Any ideas?

  • DougS's avatar
    DougS
    Khoros Oracle

    You should be able to add an attachment when you are posting a message using REST v2. Here's how you do it:

     

    Make a multipart/form-data HTTP POST to the /messages collection.  Include your JSON in a request parameter named api.request. Include your attachments in your JSON (in the attachments sub-object, where each attachment is specified as an object in the "items" array. One of the fields of each item you need to include is named field -- you specify the name of an additional request paramer here which is the request parameter of the file you are passing. Here is an example POST:

     

    POST /community/2.0/mytenantid/messages HTTP/1.1
    Host: api.lithium.com
    Cache-Control: no-cache
    Authorization: Bearer c8XWHLqYF0vpgYX4H0UzGZOolN5y+LXWV2bfZUW6QsI=
    client-id: CbEwDo2NtAhXLSt4Y49D1yVE9D371eyZFWRSSXvNLvA=
    Content-Type: multipart/form-data; boundary=----MyFormBoundary7MA4YWxkTrZu0gW
    
    ----MyFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name="api.request"
    
    { "data": { "type": "message", "subject": "here's a post with an attachment", "board": { "type": "board", "id": "studio" }, "attachments": { "list_item_type": "attachment", "items": [ { "type": "attachment", "field": "myattachment", "filename": "myfile.png" } ] } } }
    ----MyFormBoundary7MA4YWxkTrZu0gW
    Content-Disposition: form-data; name="myattachment"; filename="myfile.png"
    Content-Type: image/png
    
    
    ----MyFormBoundary7MA4YWxkTrZu0gW

    As you can see, my myfile.png file is transmitted in the "myattachment" request parameter, and so the value of the field field in my attachment in the request JSON is "myattachment".

     

    SuzieH we don't have this documented yet, but we can use this an example when we do)

     

    You can also make REST v1 calls if you are using the API proxy -- see the "URL format with OAuth" section on The API Call  page in the REST v1 docs for more information on how to do this.

     

    -Doug

    • msommer's avatar
      msommer
      Contributor

      I have tested the v2 REST call and it returns the success status. However the attachment (a zip archive) is silently discarded. Sending an equivalent request with a plain text file works as expected. Are the any problems while uploading binary files through the API proxy?

       

      I'm not very fond of using API v2 as it is marked 'Open Beta' according to the v2 developer documentation.

       

      Using REST v1 returns the error mentioned in my first message.

      • msommer's avatar
        msommer
        Contributor

        I did more tests with binary attachments. It seems that the last binary attachment is not correctly recognized / discarded. The API should at least reject the whole request if the number of attachments in the "api.request" JSON differs from the received files.