Tag Archives: API Parameter

Read REST API GET Parameters in Apex Class

URL: /services/apexrest/AccountAPI?id=AccountId
Method: Get

@RestResource (urlMapping='/AccountAPI/*')
global with sharing class AccountRESTService {
    @HttpGet
    global static Account getAccount() {
        RestRequest req = RestContext.request;
        String accountId = req.params.get('id');
        Account acc = [SELECT Id, Name FROM Account WHERE Id =: accountId];
        return acc;
    }
}

URL: /services/apexrest/AccountAPI/AccountId
Method: Get

@RestResource (urlMapping='/AccountAPI/*')
global with sharing class AccountRESTService {

    @HttpGet
    global static Account getAccount() {
        RestRequest req = RestContext.request;
        String accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        Account acc = [SELECT Id, Name FROM Account WHERE Id =: accountId];
        return acc;
    }
}