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
CloudHuggerCloudHugger 

inserting an attachment via REST

Hi All,

 

I am trying to upload an image to SF.com via REST API using Javascript.  Does anyone have a working example of what this might look like?  I am hitting a brick wall when it comes to packaging the Base64 encoded image into the payload of the header.

 

Any suggestions or assistance most appreciated.

 

dkadordkador

Are you getting any errors?

CloudHuggerCloudHugger

Hi dkador,

 

I have had a 415 error telling me this is not supported, that being a body that is base64 encoded and content-type 'image/jpeg'...

 

Have you been able to send an attachment to sf.com via REST successfully?  I am now just wondering if there is a limitation on being able to do this...

dkadordkador

Yes, I've done this.  The error indicates you're not sending JSON or XML.  For now, you need to send a JSON dictionary or XML structure that has the attachment body encoded in base64.  So if you're using JSON, your HTTP request should have a content-type of "application/json" and the body should be something like:

 

{ "name" : "attachment name", "body" : "base64encodedbinarydata", "otherfields" : "othervalues", ... }

 

Make sure you're sending the request to /vXX.X/sobjects/attachment.

 

That should do the trick.

CloudHuggerCloudHugger

Thanks dkador. It was great to hear you'd actually done this.  Based on your response I persisted and now have my app running nicely.

 

Many thanks for taking the time to assist!

cash123cash123

Hi CloudHugger,

 

Could you give some details on how you did this?

 

I need to add a file attachment to the Account object record & a Custom object record. Could you indicate the specific API call you used? Is it not true that attachements refer to their parent object, and not vice versa? So you have to work from the Attachment object for insert/update - right? What rest call is there for doing this? I am a newbie & confused as well.

 

Thanks for your help.