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(
HttpMultipartMode.BROWSER_COMPATIBLE);
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.setEntity(reqEntity);
httppost.setHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
publishResponse = salesforceRestClient.sendSync(RestMethod.POST, url,reqEntity);
NotepriseLogger.logMessage("responseBody"+publishResponse);
// 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.
Attribution to: Sandeep Agrawal
Possible Suggestion/Solution #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(
"/Users/abhinav/Downloads/chatter-rest-test.jpeg");
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",
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("Hellow World", "text/plain",
Charset.defaultCharset()));
reqEntity.addPart("feedItemFileUpload", new FileBody(imageFile,
fileName, "application/octet-stream", Charset.defaultCharset()
.toString()));
httppost.setEntity(reqEntity);
httppost.setHeader("Authorization", OAUTH_TOKEN);
String response = EntityUtils.toString(httpclient.execute(httppost)
.getEntity(), "UTF-8");
System.out.println(response);
// Time to see the glorious post in web browser
}
}
Attribution to: Abhinav Gupta
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1124