Rest API Call for GET and POST

Following is the code for making REST API Calls for GET/POST:

@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:
GET – call following URL in workbench rest explorer”https://instance.salesforce.com/services/apexrest/Account/accountId”
POST -“https://instance.salesforce.com/services/apexrest/Account/”

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

Leave a Comment