Use the System.RestContext class to access the RestRequest and RestResponse objects in your Apex REST methods.
The following example shows how to use RestContext to access the RestRequest and RestResponse objects in an Apex REST method.
@RestResource(urlMapping='/MyRestContextExample/*') global with sharing class MyRestContextExample { @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; } }
The following are properties for RestContext.
public RestRequest request {get; set;}
Type: System.RestRequest
public RestResponse response {get; set;}
Type: System.RestResponse