Forum Discussion
- Inactive User
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
- sbhambadHelper
We are unable to find any solution in the above link
- DougSKhoros 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
- pratikgadekarContributor
What is the attachement parameter to be used for upload the files?
- HaidongGLithium 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
- DougSKhoros 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
- TedCLithium 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>
- mor_etMentorIssue was solved locally.
Thanks!
Related Content
- 12 years ago
- 13 years ago
- 8 years ago