[SalesForce] Image upload to chatter post

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(
                        "E:\\NotePrise\\Salesforce\\menu.jpg");*/
                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",
                        Charset.defaultCharset()));
                // 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",
                        Charset.defaultCharset()));
                reqEntity.addPart("feedItemFileUpload", new FileBody(file,
                        fileName1, "application/octet-stream", Charset.defaultCharset()
                                .toString()));
                httppost.setEntity(reqEntity);

                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/" +
                              "feeds/user-profile/me/feed-items";
                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.getParams()));
                    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();
                    httpClient.getParams().setSoTimeout(60000);

                    int returnCode = httpClient.executeMethod(postMethod);
                    String responseBody = postMethod.getResponseBodyAsString();
                    NotepriseLogger.logMessage(responseBody+"returncode"+returnCode);

                } finally {
                    postMethod.releaseConnection();
                }

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);

Best Answer

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
1969CAg7t.vlXqyFyMd8cEh8axN1u8o86yAKBlcuyr7YOdNSnW52qNDppsduxI
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: instance_name
Content-Length: 817
Content-Type: multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq

--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."
      }
      ]
   }, 
   "attachment": 
   {
      "desc": "Quarterly review",
      "filename": "2012_q1"
   }
}
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq

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
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq--
Related Topic