You need to sign in to do that
Don't have an account?
Haris Osmanagic, B&H
Error with creating a FeedElement with an attachment: You have sent us an Illegal URL or an improperly formatted request
I'm using the code below to create a feed element with an attachment, using Chatter REST API, v32.0.
File feedElementDefinition = new File("feed3.txt");
String json = IOUtils.toString(new FileInputStream(feedElementDefinition));
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(
"https://eu3.salesforce.com/services/data/v32.0/chatter/feed-elements" +
"?feedElementType=FeedItem" +
"&subjectId=<id of my user account>" +
"&text=Text"
);
post.addHeader(HttpHeaders.AUTHORIZATION, token());
post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.getMimeType());
post.addHeader(HttpHeaders.ACCEPT, ContentType.WILDCARD.getMimeType());
post.setEntity(
MultipartEntityBuilder.create()
.addBinaryBody(
"feedElementFileUpload",
new File("feed_element.txt"),
ContentType.APPLICATION_OCTET_STREAM,
"feed_element.txt"
)
.addTextBody(
"json",
json,
ContentType.APPLICATION_JSON
).build()
);
CloseableHttpResponse response = client.execute(post);
Contents of `feed3.txt` are:
{
"body" : {
"messageSegments" : [ {
"text" : "Some dummy status",
"type" : "Text"
} ]
},
"capabilities" : {
"associatedActions" : null,
"bookmarks" : {
"isBookmarkedByCurrentUser" : false
},
"content" : {
"description" : "feed_file",
"title" : "feed3.txt"
}
},
"feedElementType" : "FeedItem",
"originalFeedElementId" : null,
"visibility" : "AllUsers",
"subjectId" : "<my user account id>"
}
However, it fails with the following message:
You have sent us an Illegal URL or an improperly formatted request.
What's the reason for this? I'm going almost crazy on this one, since when I use cURL (show below), it's fine.
curl -H "X-PrettyPrint: 1" -H "Content-Type: multipart/form-data" -F 'json={ "body" : { "messageSegments" : [ { "text" : "Some dummy status", "type" : "Text" } ] }, "capabilities" : { "associatedActions" : null, "bookmarks" : { "isBookmarkedByCurrentUser" : false }, "content" : { "description" : "feed_file", "title" : "feed3" } }, "feedElementType" : "FeedItem", "originalFeedElementId" : null, "visibility" : "AllUsers", "subjectId" : "my_user_account_id" };type=application/json' -F "feedElementFileUpload=@/home/haris/projects/open-source/java-playground/feed3.txt;type=application/octet-stream"
-X POST 'https://eu3.salesforce.com/services/data/v32.0/chatter/feed-elements?feedElementType=FeedItem&subjectId=my_user_account_id&text=Text' -H 'Authorization: OAuth my_token_blah_blah' --insecure
Thanks in advance!
File feedElementDefinition = new File("feed3.txt");
String json = IOUtils.toString(new FileInputStream(feedElementDefinition));
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(
"https://eu3.salesforce.com/services/data/v32.0/chatter/feed-elements" +
"?feedElementType=FeedItem" +
"&subjectId=<id of my user account>" +
"&text=Text"
);
post.addHeader(HttpHeaders.AUTHORIZATION, token());
post.addHeader(HttpHeaders.CONTENT_TYPE, ContentType.MULTIPART_FORM_DATA.getMimeType());
post.addHeader(HttpHeaders.ACCEPT, ContentType.WILDCARD.getMimeType());
post.setEntity(
MultipartEntityBuilder.create()
.addBinaryBody(
"feedElementFileUpload",
new File("feed_element.txt"),
ContentType.APPLICATION_OCTET_STREAM,
"feed_element.txt"
)
.addTextBody(
"json",
json,
ContentType.APPLICATION_JSON
).build()
);
CloseableHttpResponse response = client.execute(post);
Contents of `feed3.txt` are:
{
"body" : {
"messageSegments" : [ {
"text" : "Some dummy status",
"type" : "Text"
} ]
},
"capabilities" : {
"associatedActions" : null,
"bookmarks" : {
"isBookmarkedByCurrentUser" : false
},
"content" : {
"description" : "feed_file",
"title" : "feed3.txt"
}
},
"feedElementType" : "FeedItem",
"originalFeedElementId" : null,
"visibility" : "AllUsers",
"subjectId" : "<my user account id>"
}
However, it fails with the following message:
You have sent us an Illegal URL or an improperly formatted request.
What's the reason for this? I'm going almost crazy on this one, since when I use cURL (show below), it's fine.
curl -H "X-PrettyPrint: 1" -H "Content-Type: multipart/form-data" -F 'json={ "body" : { "messageSegments" : [ { "text" : "Some dummy status", "type" : "Text" } ] }, "capabilities" : { "associatedActions" : null, "bookmarks" : { "isBookmarkedByCurrentUser" : false }, "content" : { "description" : "feed_file", "title" : "feed3" } }, "feedElementType" : "FeedItem", "originalFeedElementId" : null, "visibility" : "AllUsers", "subjectId" : "my_user_account_id" };type=application/json' -F "feedElementFileUpload=@/home/haris/projects/open-source/java-playground/feed3.txt;type=application/octet-stream"
-X POST 'https://eu3.salesforce.com/services/data/v32.0/chatter/feed-elements?feedElementType=FeedItem&subjectId=my_user_account_id&text=Text' -H 'Authorization: OAuth my_token_blah_blah' --insecure
Thanks in advance!
http://stackoverflow.com/questions/27273942/improperly-formatted-request-error-after-switching-from-commons-httpclient-to