Skip to main content link. Accesskey S
  • HCL Logo
  • HCL Connections On-Premise Wiki
  • THIS WIKI IS READ-ONLY.
  • HCL Forums and Blogs
  • Home
  • API Documentation
Search
Community Articles > Best practices > IBM Connections REST API: how to add attachments
  • Share Show Menu▼

Recent articles by this author

IBM Connections REST API: how to add attachments

This article shows how to add attachments to forums and topics using the Connections REST API. It provides a server side code example for creating a topic with an attachment.
Community articleIBM Connections REST API: how to add attachments
Added by ~Julia Fronumanlen | Edited by ~Julia Fronumanlen on January 28, 2016 | Version 3
  • Actions Show Menu▼
expanded Abstract
collapsed Abstract
This article shows how to add attachments to forums and topics using the Connections REST API. It provides a server side code example for creating a topic with an attachment.
Tags: connections rest attachment upload

Background

In a recent project I did the requirement was to use the REST API to add and update forum topics and replies and to have the option to add an attachment to this topic or reply. The IBM Connections Infocenter only supplies little information on how to achieve this; no working examples. Also in the Connections forum there were only some hints and tips on how to achieve this functionality but no real code examples.

 

Scope

This article is scoped to the server side code that was used to invoke the REST API in order to upload a file into Connections. The client code needed to select a file and start an upload is out of scope for this article.

 

Prerequisites to the code

Main package dependencies are:

  • httpclient-4.3.3.jar
  • httpcore-4.3.2.jar
  • httpmime-4.3.3.jar

 

The code

String url = "protocol://hostname:port/forums/ajax/createTopic";

String boundary = "somedashes" + Long.toHexString(System.currentTimeMillis());

CloseableHttpClient httpClient = HttpClients.createDefault();

try {

  HttpPost httpPost = new HttpPost(url);

  httpPost.addHeader("Content-Type", "multipart/form-data; boundary="+boundary);

  ## Get the cookies from the request and convert these to a string

  httpPost.addHeader("Cookie", "All cookies from request as string, or ltpa cookie as string"));

  ## always add the nonce key

  httpPost.addHeader("X-Update-Nonce", “nonce key”);

  MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create().setBoundary(boundary);

  mpBuilder.addTextBody("communityId", "unique id of the community", ContentType.MULTIPART_FORM_DATA);

  ## parentId = forum id

  mpBuilder.addTextBody("parentId", "unique id of the forum", ContentType.MULTIPART_FORM_DATA);

  mpBuilder.addTextBody("name", "Topic title", ContentType.MULTIPART_FORM_DATA);

  mpBuilder.addTextBody("description", "Forum description", ContentType.MULTIPART_FORM_DATA);

  ## choose whether to mark the topic as a question

  mpBuilder.addTextBody("markAsQuestion", "false", ContentType.MULTIPART_FORM_DATA);

  mpBuilder.addTextBody("tags", "space seperated string", ContentType.MULTIPART_FORM_DATA);

  ## attachment is an inputstream containing the attached file

  mpBuilder.addBinaryBody("attachments", attachment, ContentType.APPLICATION_OCTET_STREAM, "attachment name");

  httpPost.setEntity(mpBuilder.build());

  try {

    CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

    HttpEntity entity = httpResponse.getEntity();

    ## the response contains the new created topic

    String response = EntityUtils.toString(entity);

  } finally {

    httpResponse.close();

  }

} catch (Exception e) {

       exception handling

} finally {

  try {

    httpClient.close();

  } catch (Exception e2) {

  }

}

 

Additions to the code

LTPA token is used for authentication but you can of course also use Basic Authentication.

 

In the code a header is created to hold the so called nonce key. This key is needed as an extra check to validate the upload. A detailed explanation of the nonce key and how to retrieve it can be found by clicking on the URL below:

https://www-10.lotus.com/ldd/lcwiki.nsf/xpAPIViewer.xsp?lookupName=IBM+Connections+5.0+API+Documentation#action=openDocument&res_title=Getting_a_cryptographic_key_ic50&content=apicontent

 

In order not to store the uploaded file in the JVM which can cause memory issues, it is best to use an input stream for the file upload.

 


  • Actions Show Menu▼


expanded Attachments (0)
collapsed Attachments (0)
Edit the article to add or modify attachments.
expanded Versions (1)
collapsed Versions (1)
Version Comparison     
VersionDateChanged by              Summary of changes
This version (3)Jan 28, 2016, 9:27:25 AM~Julia Fronumanlen  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedAbout
  • HCL Software
  • HCL Digital Solutions community
  • HCL Software support
  • BlogsDigital Solutions blog
  • Community LinkHCL Software forums and blogs
  • About HCL Software
  • Privacy
  • Accessibility