You have to make a form post to get this to work; you can't just make an HTTP GET passing the file name in the query string.
To illustrate, here's how you'd do it in curl (the options are put on different lines for readability):
curl --form feedItemFileUpload=@/home/me/test.txt
--form fileName=contentFile
--form "desc=This is a test file that I'm posting."
--form "text=I love posting files to Chatter!"
--header 'Authorization: OAuth 00Dx0000000X42V!AQgAQClp6UqGWNJUf4d3LQkVCysBIS6afWAGodgODJ.o1XQkE_fAnZIv5gzfRXxSf_D.28fsauDdZVE1nQU9lMqQSfOJTtb3'
https://na1.salesforce.com/services/data/v22.0/chatter/feeds/user-profile/me/feed-items
And here's how you'd do it in Java:
import java.io.File;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.*;
...
String oauthToken = "00Dx0000000X42V!AQgAQClp6UqGWNJUf4d3LQkVCysBIS6afWAGodgODJ.o1XQkE_fAnZIv5gzfRXxSf_D.28fsauDdZVE1nQU9lMqQSfOJTtb3";
String url = "https://na1.salesforce.com/services/data/v22.0/chatter/feeds/user-profile/me/feed-items";
String text = "I love posting files to Chatter!";
File contentFile = getFile();
String desc = "This is a test file that I'm posting.";
String fileName = "contentFile";
Part[] parts = {
new StringPart("desc", desc),
new StringPart("fileName", fileName),
new StringPart("text", text),
new FilePart("feedItemFileUpload", contentFile),
};
final PostMethod postMethod = new PostMethod(url);
try {
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
postMethod.setRequestHeader("Authorization", "OAuth " + oauthToken);
postMethod.addRequestHeader("X-PrettyPrint", "1");
HttpClient httpClient = new HttpClient();
httpClient.getParams().setSoTimeout(60000);
int returnCode = httpClient.executeMethod(postMethod);
System.out.println(postMethod.getResponseBodyAsString());
assertTrue("Expected return code of: " + HttpStatus.SC_CREATED, returnCode == HttpStatus.SC_CREATED);
} finally {
postMethod.releaseConnection();
}
Did you include the Authorization header in the request?
We haave added Auth in Header
from Chatter REST API File Upload Error : Chatter REST API File Upload Bad request error
Follwing is our api..We were succesfuly in posting content,comments,links etc but file upload has error
Yes Auth Header was added
Following is our API
https://prerelna1.pre.salesforce.com/services/data/v22.0/chatter/feeds/to/me/feed-items?feedItemFileUpload=C://test.txt&fileName=test
https://prerelna1.pre.salesforce.com/services/data/v22.0/chatter/feeds/to/me/feed-items?feedItemFileUpload=C:%5Ctest.txt&fileName=test
Please advise as soon as possible.
You have to make a form post to get this to work; you can't just make an HTTP GET passing the file name in the query string.
To illustrate, here's how you'd do it in curl (the options are put on different lines for readability):
And here's how you'd do it in Java:
Note in the curl example that the '@' in
is required -- see the curl doc at http://curl.haxx.se/docs/ for more info.