function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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;
		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");
				
				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);
			}
			requestJSON= new JSONObject();
			requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
			if (attachment != null)
			{
				requestJSON.putOpt("attachment", attachment);
			}
			bodyString = requestJSON.toString();
			NotepriseLogger.logMessage("Json"+requestJSON);			
		}  
		catch (JSONException e) 
		{
			e.printStackTrace();
		} 
	
		return bodyString;
	}

 Please suggest some solution for it.

Thanks

ChrisOctagonChrisOctagon

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..."

 

Thanks

 

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.

Thanks