rest api in salesforce
TRANSCRIPT
REST APIVivek DeepakSwayam Arora
Architecture1. Stateless2. Code on demand3. Easy to use
Open End Point
Client Server
Request
Response
Example :-1. Weather APIs of Yahoo, Google etc.2. Site URL from SFDC
Authorized End Point
More about REST1. GET, POST, PUT, DELETE2. The Force.com REST API supports OAuth 2.03. Friendly URLs4. Mobile Apps5. Force.com site6. Web to Anything form
SOAP API REST API
SOAP is a protocol REST is an architectural style
SOAP defines standards to be strictly followed
REST does not define too much standards like SOAP
SOAP defines its own security.
RESTful web services inherits security measures from the underlying transport
XML data format only Plain text, HTML, XML, JSON
Less Popular More Popular
Compression
• Types of Compressiono Response Compressiono Request Compression
• Compression Headerso Accept-Encoding header.o Content-Encoding header.
Hands On
Creating REST Service1. Write an Apex Class with annotation
@RestResource(urlMapping=‘</resourceURL>*')
2. Create one or more methods using below annotaions:-1. @HttpGet2. @HttpPost3. @HttpDelete4. @HttpPatch5. @HttpPut
Exposing REST Service
1. For Secured End Points: Use a Connected App
2. For Open End Points: Use Salesforce Sites
Calling REST Service1. Add Server End Point to ‘Remote Site Settings’.
2. Generate Access Token and Instance URL1. HttpRequest req = new HttpRequest();2. req.setMethod('GET');3. req.setEndPoint(endUrl+'/services/oauth2/token');4. req.setBody('grant_type=password' +
'&client_id=' + consumerKey+ '&client_secret=' + consumerSecret+ '&username=' + EncodingUtil.urlEncode(userName, 'UTF-8') + '&password=' + EncodingUtil.urlEncode(password, 'UTF-8'));
5. Http http = new Http();6. HttpResponse res = http.send(req);
Calling REST Service3. Calling the REST Api using Access Token:
1. HttpRequest req = new HttpRequest(); 2. req.setMethod(‘GET');
3. req.setHeader('Content-Type', 'application/json'); 4. req.setEndpoint(instanceUrl+'/services/apexrest/SaveCallLog'); 5. req.setHeader('Authorization', 'OAuth '+accestoken); 6. Http http = new Http(); 7. HttpResponse res = http.send(req);
Thank You!