Rest API Call for GET and POST

R

@RestResource(urlMapping=’/Account/*’)
global with sharing class MyRestResource {

@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;
}
}

OUTPUT->

  1. GET – call following URL in workbench rest explorer”https://instance.salesforce.com/services/apexrest/Account/accountId”
  2. POST -“https://instance.salesforce.com/services/apexrest/Account/”

    JSON Format-

{
“name” : “Wingo Ducks”,
“phone” : “707-555-1234”,
“website” : “www.wingo.ca.us”
}

 

 

About the author

Niranjan Chakankar
By Niranjan Chakankar

Category