Forum Discussion

sbhambad's avatar
sbhambad
Helper
11 years ago

We can not find rest API for attachment.

Hi ,

 

We are trying to find Rest API for attachement and unable to find the same. 

 

Could you please help up to fine the same>

 

Regards,

Sandeep

    • HaidongG's avatar
      HaidongG
      Lithium Alumni (Retired)

      Hi pratikgadekar,

       

      the field parameter name to post a message with restapi is "message.upload".

       

      here is my Java sample code to post a message with an an attachment via REST API. (you may need to import the latest apache http jar files)

       

          public static void main(String[] args) throws Exception {
      
              // Setup
              String communityUrl = "http://mycommunityid.stage.lithium.com";
              String restUser = "HaidongG";
              String restPassword = "my_secret_password";
      
              String communityRestUrl = communityUrl + "/restapi/vc";
      
              debug("Authentication ...");
              String loginUrl = communityRestUrl + "/authentication/sessions/login?user.login=" + restUser + "&user.password=" + restPassword;
              String authenResult = httpGet(loginUrl, null);
              String restSessionKey = extractSessionKeyFromXML(authenResult);
      
              String board = "this_is_my_board_id";
              String subject = encode("Subject 9th April");
              String body = encode("Body 9th April");
      
              debug("creating Message with attachment");
              String postUrl = communityRestUrl + "/boards/id/" + board + "/messages/post";
              String createResult = httpPostMessage(postUrl, restSessionKey, subject, body, new File("c:/tmp/test.jpg"));
              String messageId = extractMessageIdFromXML(createResult);
              debug("\n\n messageId=" + messageId + "\n\n");
          }
          
          private static String httpPostMessage(String urlStr, String restSessionKey, String subject, String body, File file) throws Exception {
      
              urlStr = addSessionKeyToURL(urlStr, restSessionKey);
              debug("Posting to " + urlStr + ", Attachment is " + file.getAbsolutePath());
      
              HttpPost post = new HttpPost(urlStr);
              MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      
              builder.addTextBody("message.subject", subject);
              builder.addTextBody("message.body", body);        
              builder.addPart("message.upload", new FileBody(file));
      
              HttpEntity entity = builder.build();
              post.setEntity(entity);
              HttpClient client = HttpClientBuilder.create().build();
              HttpResponse response = null;
              try {
                  response = client.execute(post);
              } catch (ClientProtocolException e) {
                  debug(e);
              } catch (IOException e) {
                  debug(e);
              }
      
              return getResponseString(response);
          }

       I hope that my code helps to explain better.

       

      Best Regards

      Haidong

       

       

      • DougS's avatar
        DougS
        Khoros Oracle

        I believe the logic allows any parameter name for files.  As long as the API request to post a message is a multipart/form-data request, it will take whatever files are included and add them as attachments.

         

        -Doug

  • TedC's avatar
    TedC
    Lithium Alumni (Retired)

    I spoke with some of the developers regarding creating a simple HTML form that leverages the API for attachments and this is one that ChristopherB showed me for image uploads:

     

    <form enctype="multipart/form-data" class="lia-form lia-form-horizontal" action="https://community.com/restapi/vc/messages/id/109/edit" method="post" id="form_1" name="form_1">        
            <label for="lia-image-content">Image</label>
            <input id="lia-image-content" name="message.upload" type="file" />
            <input title="" value="Upload Image" id="action.upload-image.button" name="action.upload-image" type="submit" />
    </form>