+ Start a Discussion
Maros SitkoMaros Sitko 

chatter rest api - send attachment

Hi, can you help me with post Chatter feed item with REST API?
I am able to post feeditem with attachment, but after download attachment (image),
my computer say that it is broken. So I thing, I have wrong Content-Type.
Here is my JSON

--0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8

{"body":{"messageSegments":[{"type":"text","text":" From CassaDeveloper:\nabc"}]},"attachment":{"attachmentType":"NewFile","title":"image.png"}}

--0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r 

 Content-Disposition: form-data; name="feedItemFileUpload"; filename="image.png"
Content-Type: application/octet-stream; Content-Transfer-Encoding: base64

blob of content which is from ContentVersion EncodingUtil.base64Encode((blob)file[0].VersionData)
--0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r--

 I build it with apex as a String:

list<ContentVersion> file = [SELECT id, Title, VersionData, PathOnClient FROM ContentVersion where id = :attId];
jsonBody = '--0HW....';
jsonBody += EncodingUtil.base64Encode((blob)file[0].VersionData);
jsonBody += '--0H';
....
request.setMethod('POST');
request.setHeader('Content-Type',  'multipart/form-data; boundary=0HWq8x4y4DSQ4fqjXt6MinVMyqbf1r');
request.setBody(jsonBody);
....

  Thank you

Sonam_SFDCSonam_SFDC
Hi Marco,

Could you please try keeping Content-Transfer-Encoding: binary instead of base64.

I found this code when reasearchng and seems this works:https://gist.github.com/henriquez/2776843