Apex REST Code Sample Using RestRequest
This sample shows you how to add an attachment to a record by using the RestRequest object.
For more information about authenticating with cURL, see the Quick Start section of the REST API Developer Guide. In this code, the binary file data is stored in the RestRequest object, and the Apex service class accesses the binary data in the RestRequest object .
-
Create an Apex class in your org from Setup by entering
Apex Classesin theQuick Findbox, then selecting Apex Classes. Click New and add the following code to your new class: -
Open a command-line window and execute the following
cURLcommand to upload the attachment to a case:curl -H "Authorization: BearersessionId" -H "X-PrettyPrint: 1" -H "Content-Type: image/jpeg" --data-binary @file"https://MyDomainName.my.salesforce.com/services/apexrest/CaseManagement/v1/caseId"- Replace sessionId with the
<sessionId>element that you noted in the login response. - Replace file with the path and file name of the file you want to attach.
- Replace MyDomainName with the My Domain name for your org.
- Replace caseId with the ID of the case you want to add the attachment to. Your command should look something like this (with the sessionId replaced with your session ID and MyDomainName replaced with the My Domain Name for your org):
The
cURLexamples in this section don’t use a namespaced Apex class so you won’t see the namespace in the URL.The Apex class returns a JSON response that contains the attachment ID such as the following:
- Replace sessionId with the
-
To verify that the attachment and the image were added to the case, navigate to Cases and select the All Open Cases view. Click on the case and then scroll down to the Attachments related list. You should see the attachment you just created.