Forum Discussion
12 Replies
- Inactive User11 years ago
Check out Lithium's REST documentation for attachments. I haven't used them before via REST but hope this helps. http://lithosphere.lithium.com/t5/rest-api/bd-p/developers-rest-api?branch=Attachment
- sbhambad11 years agoHelper
We are unable to find any solution in the above link
- DougS11 years agoKhoros Oracle
There is a bug in the docs -- you can't have a space at the end of the URL -- try this instead:
http://lithosphere.lithium.com/t5/rest-api/bd-p/developers-rest-api?branch=Attachment
Thanks,
-Doug
- unixguru2k11 years agoContributorHi Doug,
We are trying to upload an attachment, the link above seems to list the attachments.
Is there an API call to upload an attachment for an existing or a new message.
Thanks
Harshal - DougS11 years agoKhoros Oracle
Hi Harshal,
You can add 1 or more attachments to any normal REST API post/edit/reply call by making the request using "multipart/form-data" for the content type and including 1 or more files as POST parameters. Here are links to the standard post, reply, and edit calls for a board:
- pratikgadekar11 years agoContributor
What is the attachement parameter to be used for upload the files?
- HaidongG11 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
- HaidongG11 years agoLithium Alumni (Retired)
yes, Doug is right. any file name will do.
I have just tested with
builder.addPart("message.uploadxxxxxxxxx", new FileBody(file));
and my file got uploaded via REST too.
- mor_et11 years agoMentor
Any clue how this would be done in .NET?
Related Content
- 12 years ago
- 13 years ago
- 8 years ago