I want to add image to the content which I am posting to chatter. I am using chatter rest api but not able to do it..

Any kind of help will be greatful.

I am using below code for uploading image to chatter post using restapi.

if (imageFile != null && fileName != null && imageTitle != null)
                String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost ;
                MultipartEntity reqEntity ;
                httppost = new HttpPost(url);
                reqEntity = new MultipartEntity(
                FileBody bin = new FileBody(imageFile);
                reqEntity.addPart("feedItemFileUpload", bin);
                reqEntity.addPart("fileName", new StringBody(fileName, "text/html", Charset.defaultCharset()));
                reqEntity.addPart("text", new StringBody(noteContent, "text/html", Charset.defaultCharset()));
                reqEntity.addPart("feedItemFileUpload", new FileBody(imageFile, fileName, "application/octet-stream", Charset.defaultCharset().toString()));
                httppost.setHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
                publishResponse = salesforceRestClient.sendSync(RestMethod.POST, url,reqEntity);

// HttpResponse response = httpclient.execute(httppost); //

I am getting below error: 09-03 13:17:45.822: V/Noteprise(1619): 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!

Some time I am getting IOException as well.

1 Answer 1


I was able to copy and execute your code as standalone java project, here is the code it works like charm and creates the chatter post with image and text. Seems this is because of the way HTTP request is composed by SalesforceRestClient. Try sending the post request using HTTP Client lib directly i.e. without SalesforceRestClient, it might work

import java.io.File;
import java.nio.charset.Charset;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Test {
    // TODO:change node from ap1 to your node
    private static final String RES_URL = "https://ap1.salesforce.com/services/data/v25.0/chatter/feeds/news/me/feed-items";
    // TODO:change this before run
    private static final String OAUTH_TOKEN = "OAuth <YOUR OATUH TOKEN GOES HERE>";

    public static void main(String[] args) throws Exception {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost;
        MultipartEntity reqEntity;
        httppost = new HttpPost(RES_URL);
        reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        File imageFile = new File(
        FileBody bin = new FileBody(imageFile);
        reqEntity.addPart("feedItemFileUpload", bin);
        String fileName = "chatter-rest-test.jpeg";
        // file name can be text plain only, though using text/html doesn't breaks
        reqEntity.addPart("fileName", new StringBody(fileName, "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("Hellow World", "text/plain",
        reqEntity.addPart("feedItemFileUpload", new FileBody(imageFile,
                fileName, "application/octet-stream", Charset.defaultCharset()

        httppost.setHeader("Authorization", OAUTH_TOKEN);

        String response = EntityUtils.toString(httpclient.execute(httppost)
                .getEntity(), "UTF-8");

        // Time to see the glorious post in web browser
  • I had already tried this method but still it is not working in my case. Commented Sep 5, 2012 at 5:36
  • Hi Abhinav, can you please suggest how to get "OAUTH_TOKEN" in above Java code? Commented Sep 6, 2012 at 10:57
  • Salesforce Session ID works well as OAUTH token. SO, login via web browser to the targeted org and copy the value of SID cookie for the correct domain. Commented Sep 6, 2012 at 13:33
  • @AbhinavGupta : tried above code and getting below error. Is there anything else missing in above code? Binary data included but file attachment information is missing. If providing rich JSON/XML input in multipart REST, make sure to include Content-Type header in the part.","errorCode":"POST_BODY_PARSE_ERROR" Commented Sep 23, 2013 at 19:12
  • To add : I get above error when I try to POST (Content Post) on account or contact record, but when I use same code/logic to post on News Feed (User Profile) there's no error. Commented Sep 23, 2013 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .