Forum Discussion
pratikgadekar
11 years agoContributor
What is the attachement parameter to be used for upload the files?
HaidongG
11 years agoLithium 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
- DougS11 years agoKhoros 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
Related Content
- 12 years ago
- 13 years ago
- 8 years ago