Skip to content

Instantly share code, notes, and snippets.

@henriquez
Created May 23, 2012 18:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save henriquez/2776843 to your computer and use it in GitHub Desktop.
Save henriquez/2776843 to your computer and use it in GitHub Desktop.
Example feed item post with a file attachment to the Chatter REST API
authorization: OAuth 00DU0000000J83v!AQoAQJkLzYuCPmfyTZ7Mmg.Otq3W6klMTqqXUsTUdfwMtk8BcZJxpNdyCfUmm5.0Psfr8j7xDoNnRuV8u7UtANm.E_ubaXCB
x-chatter-entity-encoding: false
accept: */*
user-agent: Ruby
content-type: multipart/form-data; boundary=-----------RubyMultipartPost
content-length: 34684
-------------RubyMultipartPost
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit
{"body":{"messageSegments":[{"text":"this is the body ","type":"text"},{"type":"mention","id":"005U0000000GIQkIAO"}]},"attachment":{"title":"this is the file name","desc":"this is the file description","fileName":"Screen shot 2012-05-19 at 4.11.12 PM.png"}}
-------------RubyMultipartPost
Content-Disposition: form-data; name="feedItemFileUpload"; filename="Screen shot 2012-05-19 at 4.11.12 PM.png"
Content-Type: image/png
Content-Transfer-Encoding: binary
# binary file attachment here
-------------RubyMultipartPost--
@henriquez
Copy link
Author

Example HTTP POST content when posting a feed item to the Chatter REST API with a file attachment.

@sandeep-agrawal
Copy link

Can you please give me a sample code in java for android ?
Thanks

@sandeep-agrawal
Copy link

I am trying below code but getting 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!
"

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

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

Any input will help me.
Thanks

@sandeep-agrawal
Copy link

dcf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment