Use the System.RestRequest class to pass request data into an Apex RESTful Web service method that is defined using one of the REST annotations.
@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 constructors for RestRequest.
The following are properties for RestRequest.
While the RestRequest List and Map properties are read-only, their contents are read-write. You can modify them by calling the collection methods directly or you can use of the associated RestRequest methods shown in the previous table.
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 /services/apexrest/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.
At runtime, you typically don't need to add a header or parameter to the RestRequest object because they are automatically deserialized into the corresponding properties. The following methods are intended for unit testing Apex REST classes. You can use them to add header or parameter values to the RestRequest object without having to recreate the REST method call.
public Void addHeader(String name, String value)
Type: Void
This method is intended for unit testing of Apex REST classes.