Here just shared the experiences for how to make a call to REST method from Apex side.
Firstly, we should create a Class with REST annotation. An example REST class and in below
@RestResource(urlMapping='/ReqAccounts/*')
global with sharing class RESTController {
@HttpGet
global static List<Account> getAccount() {
List<Account> accList = [ Select ID, Name from Account limit 10];
return accList ;
}
}
After that, we should config the Remote site setting to make sure the site we call from is ready for the call.
Finally, we make a call from this REST method to get Account records. Sample code as below:
<pre name="code" class="java">HttpRequest req = new HttpRequest();
req.setEndpoint('https://cs6.salesforce.com/services/apexrest/ReqAccounts');
req.setMethod('GET');
system.debug('## ' + UserInfo.getSessionId());
String authorizationHeader = 'Bearer ' + UserInfo.getSessionId();
req.setHeader('Authorization', authorizationHeader);
Http http = new Http();
HTTPResponse res = http.send(req);System.debug(res.getBody());