An Apex RESTful Web service method is defined using one of the REST annotations. For more information about Apex RESTful Web service, seeĀ Exposing Apex Classes as REST Web Services.
@RestResource(urlMapping='/Account/*') global with sharing class MyRestResource { @HttpDelete global static void doDelete() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); Account account = [SELECT Id FROM Account WHERE Id = :accountId]; delete account; } @HttpGet global static Account doGet() { RestRequest req = RestContext.request; RestResponse res = RestContext.response; String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1); Account result = [SELECT Id, Name, Phone, Website FROM Account WHERE Id = :accountId]; return result; } @HttpPost global static String doPost(String name, String phone, String website) { Account account = new Account(); account.Name = name; account.phone = phone; account.website = website; insert account; return account.Id; } }
The following are properties for RestRequest.
public String httpMethod {get; set;}
public String remoteAddress {get; set;}
Type: String
public Blob requestBody {get; set;}
Type: Blob
If the Apex method has no parameters, then Apex REST copies the HTTP request body into the RestRequest.requestBody property. If there are parameters, then Apex REST attempts to deserialize the data into those parameters and the data won't be deserialized into the RestRequest.requestBody property.
public String requestURI {get; set;}
Type: String
For example, if the request string is https://instance.salesforce.com/services/apexrest/Account/ then the requestURI is /Account/.
public String resourcePath {get; set;}
Type: String
For example, if the Apex REST class defines a urlMapping of /MyResource/*, the resourcePath property returns /services/apexrest/MyResource/*.
The following are methods for RestRequest. All are instance methods.
public Void addHeader(String name, String value)
Type: Void
This method is intended for unit testing of Apex REST classes.