+ Start a Discussion
Sandeep AgrawalSandeep Agrawal 

Jave code to post image to chatter user using rest api

I need java code to post a image to chatter user using rest api.


I am using below code but it is not working for me:

Part[] parts = { 
									//new StringPart("desc", "Description"),
									new StringPart("body",stringBody),
									//new StringPart("fileName", fileName),
									//new StringPart("text", noteContent),																				
									new FilePart("feedItemFileUpload", imageFile),
				    postMethod = new PostMethod(url);
					postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
				postMethod.setRequestHeader("Content-type", "multipart/form-data");
				org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
				int code =client.executeMethod(postMethod);

public static String generateJSONBodyForChatterFeed(String content, ArrayList<String> mentionIds, String imageDescription, String fileName, String imageTitle)
		JSONArray msg = new JSONArray();
		String bodyString = null;
		JSONObject requestJSON=null;
			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));
			if (content != null)
				JSONObject text = new JSONObject();
				text.put("type", "text");
				text.put("text", " " + content);
			JSONObject attachment = null;
			if (fileName != null && imageTitle != null)
				attachment = new JSONObject();
				attachment.putOpt("desc", imageDescription);
				attachment.putOpt("fileName", fileName);
				attachment.putOpt("title", imageTitle);
			requestJSON= new JSONObject();
			requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
			if (attachment != null)
				requestJSON.putOpt("attachment", attachment);
			bodyString = requestJSON.toString();
		catch (JSONException e) 
		return bodyString;

 Please suggest some solution for it.



How is generateJSONBodyForChatterFeed() called? It looks like you're setting "body" as a StringPart, which won't work, as that part needs a content-type of application/json...

Sandeep AgrawalSandeep Agrawal

Stringbody will be having the out put of generateJSONBodyForChatterFeed() which will be string format of json.


what modification I need to do  for this "It looks like you're setting "body" as a StringPart, which won't work, as that part needs a content-type of application/json..."




Sandeep AgrawalSandeep Agrawal

 I am getting below error

"[{"message":"Creating a text post requires text","errorCode":"MISSING_ARGUMENT"}]"


If I remove below line :

postMethod.setRequestHeader("Content-type", "multipart/form-data");


it is giving this error

[{"message":"Text posts should not include binary data.","errorCode":"POST_BODY_PARSE_ERROR"}]


Please let me know working code if you have.