I am trying to send image to chatter post from my Android app and I am using below code to do this.

String url ="https://ap1.salesforce.com/services/data/v25.0/chatter/feeds/news/me/feed-items";

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost;
                MultipartEntity reqEntity;
                httppost = new HttpPost(url);
                reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                File file = new File("/mnt/sdcard/save_icon.png"); 
                NotepriseLogger.logMessage("File can read=="+file.canRead() + "--" + file.length());
               /* File imageFile1 = new File(
                FileBody bin = new FileBody(file);
                reqEntity.addPart("feedItemFileUpload", bin);
                String fileName1 = "Chatter Image";
                // file name can be text plain only, though using text/html doesn't breaks
                reqEntity.addPart("fileName", new StringBody(fileName1, "text/plain",
                // Sending text/html doesn't helps as HTML will be printed, though using text/html doesn't breaks
                reqEntity.addPart("text", new StringBody("Hello World", "text/plain",
                reqEntity.addPart("feedItemFileUpload", new FileBody(file,
                        fileName1, "application/octet-stream", Charset.defaultCharset()

                httppost.setHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
                HttpResponse resp = httpclient.execute(httppost);

but I am getting following error "09-10 13:58:18.118: V/Noteprise(13641): An error has occurred while processing your request. The salesforce.com support team has been notified of the problem. If you believe you have additional information that may be of help in reproducing or correcting the error, please contact Salesforce Support. Please indicate the URL of the page you were requesting, any error id shown on this page as well as any other related information. We apologize for the inconvenience.

Thank you again for your patience and assistance. And thanks for using salesforce.com!

I ran the same code as a java standalone application and it is working fine so I assumed that httpclient used in android.jar file may be causing the issue and I am trying to build my own httpclient jar with different package name so I can use that in the code.

I don't know whether it is a correct approach or not.

Any input will be helpful for me.

How can I make a jar file of httpclient source code with different package . I did it but I am getting exception because of the logger classes in it.

I tried below method as well and getting this error "09-11 16:36:42.510: W/HttpMethodBase(4399): Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. 09-11 16:36:42.900: V/Noteprise(4399): [ { 09-11 16:36:42.900: V/Noteprise(4399): "message" : "Creating a text post requires text", 09-11 16:36:42.900: V/Noteprise(4399): "errorCode" : "MISSING_ARGUMENT" 09-11 16:36:42.900: V/Noteprise(4399): } ]returncode400 "

String url = "https://ap1.salesforce.com/services/data/v25.0/chatter/" +
                String text = "I love posting files to Chatter!";

                String desc = "This is a test file that I'm posting.";

                Part[] parts = {
                        new StringPart("desc", desc),
                        new StringPart("fileName", fileName),
                        new StringPart("text", text),
                        new FilePart("feedItemFileUpload", imageFile),

                final PostMethod postMethod = new PostMethod(url);

                try {
                    postMethod.setRequestEntity(new MultipartRequestEntity(parts, 
                    postMethod.setRequestHeader("Authorization", "Bearer " + salesforceRestClient.getAuthToken());
                    postMethod.setRequestHeader("Content-type", "x-www-form-urlencoded");
                    postMethod.addRequestHeader("X-PrettyPrint", "1");

                    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();

                    int returnCode = httpClient.executeMethod(postMethod);
                    String responseBody = postMethod.getResponseBodyAsString();

                } finally {

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";
                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();
                String responseBody = postMethod.getResponseBodyAsString();

generateJSONBodyForChatterFeed() method returning json String to me.

Here is the code of this method:

JSONArray msg = new JSONArray();
    String bodyString = 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");
            //content = URLEncoder.encode(content, "UTF-8");
            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);
        JSONObject requestJSON = new JSONObject();
        requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
        if (attachment != null)
            requestJSON.putOpt("attachment", attachment);
        bodyString = requestJSON.toString();
  • Were you able to make other calls successfully to the Chatter REST API? Can you provide the raw HTTP request retrieved using Wireshark?
    – Chris S
    Sep 11, 2012 at 1:07
  • I am able to post text on the chatter so it is working fine when I am posting text only but when I am doing it with image, it is not working. Sep 11, 2012 at 5:28
  • I tried below method as well but got a error "" Sep 11, 2012 at 11:09
  • Can you post the output from your generateJSONBodyForChatterFeed() method?
    – metadaddy
    Sep 12, 2012 at 7:22
  • It is the string output of this function "Json{"body":{"messageSegments":[{"type":"text","text":" sandeep"}]},"attachment":{"fileName":"plus_icon.png","title":"Chatter!"}}" Sep 12, 2012 at 11:32

1 Answer 1


According to the documentation for uploading binary files to Chatter, you should POST a message of content type multipart/form-data with two parts. One part, with the name feedItemFileUpload contains the binary data; the other, named json or xml, contains the JSON- or XML-formatted message body. In both of your code samples, you are sending the filename, description, text etc each in separate parts, when all of these should be encoded in the single JSON part.

Here is the complete sample HTTP message from the documentation - you can see the two parts, with the message body, filename and file description in the JSON part:

POST /services/data/v25.0/chatter/feeds/user-profile/005x0000001T9PwAAK/feed-items HTTP/1.1
Authorization: Bearer 00Dx00000001hIF!AQ8AQKy6K1Tmko19Au4U29YanMFt53ccip
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: instance_name
Content-Length: 817
Content-Type: multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq

Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit

{ "body":
      "messageSegments" : [
         "type" : "Text", 
         "text" : "High priority content "
      }, {
         "type" : "Hashtag", 
         "tag" : "important"
      }, {
         "type" : "Text", 
         "text" : "Please review this as soon as possible."
      "desc": "Quarterly review",
      "filename": "2012_q1"

Content-Disposition: form-data; name="feedItemFileUpload"; filename="2012_q1_review.ppt"
Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Transfer-Encoding: binary

binary stream of file
  • When I am sending using content type multipart/form-data, it is giving error and that is why I commented that part and it is working to post image to my own wall but when I posting to user wall, it is not posting so can you tell me what changes I need to do in my code to make it work for user and group as well. Thanks Sep 12, 2012 at 5:20
  • When I am using the json or XML tag and passing the formatted json and xml and setting the content-type to multipart/form-data . it is giving me error "[{"message":"Creating a text post requires text","errorCode":"MISSING_ARGUMENT"}]" and when I commenting multipart line, it is giving me "[{"message":"Text posts should not include binary data.","errorCode":"POST_BODY_PARSE_ERROR"}]" I don't know what is the exact issue on it? Sep 12, 2012 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.