How can I call the HTTP REST API and show the data at the page loading time? I developed the code which has button and that I need to hit each time to get the results, instead I want to have the that data at page loading time.

public class ApexRestController {
    public List<CaseDetail> caseList {get; set;}

    public static String getAccessToken(){
        HttpRequest req = new HttpRequest();

        String CLIENT_ID = 'xx';
        String CLIENT_SECRET = 'xx';
        String USERNAME = 'xx';
        String PASSWORD = 'XX';

        req.setBody('grant_type=password' + '&client_id='+CLIENT_ID + 
            '&client_secret='+CLIENT_SECRET + '&username='+USERNAME + '&password='+PASSWORD);

        Http http = new Http();
        HTTPResponse res = http.send(req);
        return getAccessTokenValue(res.getBody());

     public static String getAccessTokenValue(String jsonInput){
         Map<String, Object> data = (Map<String, Object>) JSON.deserializeUntyped(jsonInput);
         return (String) data.get('access_token');

    public PageReference getCaseDetailsById(){
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'Bearer '+getAccessToken());

        HTTPResponse res = http.send(req);
        String json = res.getBody();

        CaseDetail ca = (CaseDetail) System.JSON.deserialize(json.replace('"type"','"type_Z"'), CaseDetail.class);
        caseList = new List<CaseDetail>();
        System.debug('Case Details : '+caseList);
        return null;


<apex:page controller="ApexRestController">
    <apex:form >
        <apex:pageBlock >
            <apex:commandButton reRender="table1" value="Get Case Details" action="{!getCaseDetailsById}"/>

        <apex:pageBlock id="table1">
            <b>Output Contact Details </b>
            <apex:pageBlockTable value="{!caseList}" var="ca">
                <apex:column headerValue="Id" value="{!ca.Id}" />
                <apex:column headerValue="CaseNumber" value="{!ca.CaseNumber}" />
                <apex:column headerValue="Subject" value="{!ca.Subject}" />
                <apex:column headerValue="Status" value="{!ca.Status}" />
                <apex:column headerValue="Priority" value="{!ca.Priority}" />
                <apex:column headerValue="Type" value="{!ca.Attributes.type_Z}" />
                <apex:column headerValue="URL" value="{!ca.Attributes.url}" />


Result: enter image description here


According to your requirement you should use apex:page action as follows:

<apex:page controller="ApexRestController" action="{!getCaseDetailsById}">

The action method invoked when this page is requested by the server. Use expression language to reference an action method. For example, action="{!doAction}" references the doAction() method in the controller. If an action isn’t specified, the page loads as usual. If the action method returns null, the page simply refreshes.

This method is called before the page is rendered, and allows you to optionally redirect the user to another page.

Important: This action should not be used for initialization or DML.

  • 2
    Such a great solution. Appreciated sir. !! May 19 '17 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.