Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

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 .
  1. Create an Apex class in your org from Setup by entering Apex Classes in the Quick Find box, then selecting Apex Classes. Click New and add the following code to your new class:
    1@RestResource(urlMapping='/CaseManagement/v1/*')
    2global with sharing class CaseMgmtService
    3{
    4
    5    @HttpPost
    6    global static String attachPic(){
    7        RestRequest req = RestContext.request;
    8        RestResponse res = Restcontext.response;
    9        Id caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
    10        Blob picture = req.requestBody;
    11        Attachment a = new Attachment (ParentId = caseId,
    12                                       Body = picture,
    13                                       ContentType = 'image/jpg',
    14                                       Name = 'VehiclePicture');
    15        insert a;
    16        return a.Id;
    17   }
    18}
  2. Open a command-line window and execute the following cURL command to upload the attachment to a case:
    curl -H "Authorization: Bearer sessionId" -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 MyDomainName with the My Domain name for your org.
    • Replace caseId with the ID of the case you want to add the attachment to.
    • Replace file with the path and file name of the file you want to attach.

    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):

    1curl -H "Authorization: Bearer sessionId" 
    2-H "X-PrettyPrint: 1" -H "Content-Type: image/jpeg" --data-binary @c:\test\vehiclephoto1.jpg 
    3"https://MyDomainName.my.salesforce.com/services/apexrest/CaseManagement/v1/500D0000003aCts"

    The cURL examples in this section don’t use a namespaced Apex class so you won’t see the namespace in the URL.

    Note

    The Apex class returns a JSON response that contains the attachment ID such as the following:

    1"00PD0000001y7BfMAI"
  3. 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.