Newer Version Available
Apex REST Basic Code Sample
This sample shows you how to implement a simple REST API in Apex that handles three different HTTP request methods. For more
information about authenticating with cURL, see the Quick Start section
of the REST API Developer's
Guide.
- Create an Apex class in your instance from Setup, by clicking and
add this code to your new class:
1swfobject.registerObject("clippy.codeblock-0", "9"); 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17@RestResource(urlMapping='/Account/*') 18global with sharing class MyRestResource { 19 20 @HttpDelete 21 global static void doDelete() { 22 RestRequest req = RestContext.request; 23 RestResponse res = RestContext.response; 24 String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); 25 Account account = [SELECT Id FROM Account WHERE Id = :accountId]; 26 delete account; 27 } 28 29 @HttpGet 30 global static Account doGet() { 31 RestRequest req = RestContext.request; 32 RestResponse res = RestContext.response; 33 String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); 34 Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId]; 35 return result; 36 } 37 38 @HttpPost 39 global static String doPost(String name, 40 String phone, String website) { 41 Account account = new Account(); 42 account.Name = name; 43 account.phone = phone; 44 account.website = website; 45 insert account; 46 return account.Id; 47 } 48} - To call the doGet method
from a client, open a command-line window and execute the following cURL command to retrieve an account by
ID: curl -H "Authorization: Bearer sessionId" "https://instance.salesforce.com/services/apexrest/Account/accountId"
- Replace sessionId with the <sessionId> element that you noted in the login response.
- Replace instance with your <serverUrl> element.
- Replace accountId with the ID of an account which exists in your organization.
After calling the doGet method, Salesforce returns a JSON response with data such as the following:
1{ 2 "attributes" : 3 { 4 "type" : "Account", 5 "url" : "/services/data/v22.0/sobjects/Account/accountId" 6 }, 7 "Id" : "accountId", 8 "Name" : "Acme" 9 10} - Create a file called account.txt to contain
the data for the account you will create in the next step.
1{ 2 "name" : "Wingo Ducks", 3 "phone" : "707-555-1234", 4 "website" : "www.wingo.ca.us" 5} - Using a command-line window, execute the following cURL command to create a new account:
curl -H "Authorization: Bearer sessionId" -H "Content-Type: application/json" -d @account.txt "https://instance.salesforce.com/services/apexrest/Account/"
After calling the doPost method, Salesforce returns a response with data such as the following:
1"accountId"The accountId is the ID of the account you just created with the POST request.
- Using a command-line window, execute the following cURL command to delete an account by
specifying the ID:
curl —X DELETE —H "Authorization: Bearer sessionId" "https://instance.salesforce.com/services/apexrest/Account/accountId"