function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SnovakovichSnovakovich 

Insert attachment with REST API

I'm using C# and SF REST API. I have no problems so far get-ing and inserting some custom data from SF (Accounts,etc..). But now a have to insert PDF attachment using REST API, and i'm stuck. I's  it even possible? And if it is how? Any help will be appreciated.

dkadordkador

You can either base64 encode your binary data and include it when you post a new attachment to /sobjects/attachment or you can do a multipart request.  See the documentation for details: http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sobject_insert_update_blob.htm

SpanishRedSpanishRed

Snovakovich - did you ever figure out how to add attachments using C# and the Rest API?  I've been able to figure out how to create/update data using C# and the Rest API but I just received a new requirement to insert attachments.  If you were able to figure this out and can post any code that would be helpful, it would be very much appreciated.  Thanks.

Laurent LemireLaurent Lemire
I just figured out how to do it and since I didn't find ANY documentation on how to do it in C#, I'll just post my code here in case someone still needs it.
 
String pdf = Convert.ToBase64String(File.ReadAllBytes(/*Path to your pdf file*/));
            String accountId = "0011N00001EWqShQAL";
            String name = "testAttachment.pdf";

            StringBuilder jsonData = new StringBuilder("{");
            jsonData.Append("\"Name\" : \"" + name + "\",");
            jsonData.Append("\"Body\" : \"" + pdf + "\",");
            jsonData.Append("\"parentId\" : \"" + accountId + "\"");
            jsonData.Append("}");

            HttpContent addAttachmentBody = new StringContent(jsonData.ToString(), Encoding.UTF8, "application/json");

            HttpClient apiCallClient = new HttpClient();
            String restCallUrl = instance_url + "/services/data/v33.0/sobjects/attachment/";

            HttpRequestMessage apiRequest = new HttpRequestMessage(HttpMethod.Post, restCallUrl);
            apiRequest.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            apiRequest.Headers.Add("Authorization", "Bearer " + _token /*Your OAuth token*/);
            apiRequest.Content = addAttachmentBody;

            HttpResponseMessage apiCallResponse = await apiCallClient.SendAsync(apiRequest);

            String requestResponse = await apiCallResponse.Content.ReadAsStringAsync();

the only package you need are System.Net.Http and System.IO.
Jesse Smith 19Jesse Smith 19
Laurent Lemire, excelent solution. Thank you!
Brian Oconnell 24Brian Oconnell 24
 http://www.salesforce.com/us/developer/docs/api_rest/Content/dome_sobject_insert_update_blob.htm
is now a dead link.  Thanks Salesforce!
Robert Ricker 3Robert Ricker 3
All - The link mentioned above can now be found here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm
Jyoti kumari 57Jyoti kumari 57
Can anyone tell how to send image attachments from jira to salesforce? 
Butesh SinglaButesh Singla

Hi,

Follow this link. Works fine for me :