I am trying below code to post image on user chatter wall
if (imageFile != null && fileName != null && imageTitle != null)
{
String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
Part[] parts = {
//new StringPart("desc", "Description"),
new StringPart("fileName", fileName),
//new StringPart("text", noteContent),
new StringPart("body", generateJSONBodyForChatterFeed(noteContent, selectedIds, null, fileName, imageTitle)),
new FilePart("feedItemFileUpload", imageFile),
};
//String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
NotepriseLogger.logMessage(url);
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
//postMethod.setr(generateJSONBodyForChatterFeed(noteContent, null, null, null, null));
//postMethod.setRequestHeader("Content-type", "multipart/form-data");
//postMethod.addRequestHeader("X-PrettyPrint", "1");
org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
client.executeMethod(postMethod);
String responseBody = postMethod.getResponseBodyAsString();
NotepriseLogger.logMessage(responseBody);
generateJSONBodyForChatterFeed() method returning json String to me.
Here is the code of this method:
JSONArray msg = new JSONArray();
String bodyString = null;
try
{
if (mentionIds != null)
{
for (int i = 0; i < mentionIds.size(); i++)
{
JSONObject mention = new JSONObject();
mention.put("type", "mention");
mention.put("id", mentionIds.get(i));
msg.put(mention);
}
}
if (content != null)
{
JSONObject text = new JSONObject();
text.put("type", "text");
//content = URLEncoder.encode(content, "UTF-8");
text.put("text", " " + content);
msg.put(text);
}
JSONObject attachment = null;
if (fileName != null && imageTitle != null)
{
attachment = new JSONObject();
attachment.putOpt("desc", imageDescription);
attachment.putOpt("fileName", fileName);
attachment.putOpt("title", imageTitle);
}
JSONObject requestJSON = new JSONObject();
requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
if (attachment != null)
{
requestJSON.putOpt("attachment", attachment);
}
bodyString = requestJSON.toString();
NotepriseLogger.logMessage(bodyString);
Thanks