---
schema_version: "1"
id: d828e49672a65fe05911d988f9248827963d4bcf30dd5c27644b0fea01035cb7
title: Apex REST Code Sample Using RestRequest
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexcode/apex_rest_code_sample_restrequest.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_code_sample_restrequest.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: d969ee785d45ad1f0049f6f0a9025e4c0dc46684ed34b161a382599708d7c5c1
description: This sample shows you how to add an attachment to a record by using the RestRequest object.
---
# 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](https://developer.salesforce.com/docs/atlas.en-us.264.0.api_rest.meta/api_rest/quickstart.htm "HTML (New Window)") 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:
    
    @RestResource(urlMapping='/CaseManagement/v1/\*')
    global with sharing class CaseMgmtService
    {
    
        @HttpPost
        global static String attachPic(){
            RestRequest req = RestContext.request;
            RestResponse res = Restcontext.response;
            Id caseId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
            Blob picture = req.requestBody;
            Attachment a = new Attachment (ParentId = caseId,
                                           Body = picture,
                                           ContentType = 'image/jpg',
                                           Name = 'VehiclePicture');
            insert as user a;
            return a.Id;
       }
    }
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 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):
    
    curl -H "Authorization: Bearer sessionId" 
    -H "X-PrettyPrint: 1" -H "Content-Type: image/jpeg" --data-binary @c:\\test\\vehiclephoto1.jpg 
    "https://MyDomainName.my.salesforce.com/services/apexrest/CaseManagement/v1/500D0000003aCts"
    
    ![Note](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/images/icon_note.png)
    
    #### Note
    
    The cURL examples 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:
    
    "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.