+ Start a Discussion
sony sonusony sonu 

REST CALLOUTS VISUALFROCE PAGE

public class AnimalsCallouts {
    public static HttpResponse makeGetCallout() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
            List<Object> animals = (List<Object>) results.get('animals');
            System.debug('Received the following animals:');
            for (Object animal: animals) {
                System.debug(animal);
            }
        }
        return response;
    }
    public static HttpResponse makePostCallout() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody('{"name":"mighty moose"}');
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                response.getStatusCode() + ' ' + response.getStatus());
        } else {
            System.debug(response.getBody());
        }
        return response;
    }        
}
HOW TO CREATE A VISUALFORCE PAGE FOR THIS APEX CLASS??? WITH TWO BUTTONS OF POST AND GET????
THANKS >>>
 
Best Answer chosen by sony sonu
Amit Chaudhary 8Amit Chaudhary 8
You need to do some changes in your code like below

Apex class
public class AnimalsCallouts {

    // Unique label corresponding to the continuation
    public String requestLabel;
    public String result {get;set;}
    public List<Object> animals {get;set;}
    
    public AnimalsCallouts(){ 
        animals =new List<Object>();
    }

    
    public Object  makeGetCallout() {
        Continuation con = new Continuation(40);
        con.continuationMethod='processResponse';
    
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
    
        // Add callout request to continuation
        this.requestLabel = con.addHttpRequest(request);
        // Return the continuation
        return con; 
    }
    
    public Object processResponse() {  
        HttpResponse response = Continuation.getResponse(this.requestLabel);

        // Set the result variable that is displayed on the Visualforce page
        if (response.getStatusCode() == 200) {
            this.result = response.getBody();
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            animals = (List<Object>) results.get('animals');
        }
        return null;
    }
        
}
Visual Force Page
<apex:page controller="AnimalsCallouts">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!makeGetCallout}" value="GetCall" reRender="detail"/>
                
            </apex:pageBlockButtons>
            
            <apex:PageBlockSection id="detail">
                 {!result }
                 <apex:pageBlockTable value="{!animals }" var="an">
                     <apex:column value="{!an}"/>
                 </apex:pageBlockTable>
                 
            </apex:PageBlockSection>
        
        </apex:pageBlock>
    
    </apex:form>


</apex:page>

Output will look like below
User-added image

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
You need to do some changes in your code like below

Apex class
public class AnimalsCallouts {

    // Unique label corresponding to the continuation
    public String requestLabel;
    public String result {get;set;}
    public List<Object> animals {get;set;}
    
    public AnimalsCallouts(){ 
        animals =new List<Object>();
    }

    
    public Object  makeGetCallout() {
        Continuation con = new Continuation(40);
        con.continuationMethod='processResponse';
    
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
    
        // Add callout request to continuation
        this.requestLabel = con.addHttpRequest(request);
        // Return the continuation
        return con; 
    }
    
    public Object processResponse() {  
        HttpResponse response = Continuation.getResponse(this.requestLabel);

        // Set the result variable that is displayed on the Visualforce page
        if (response.getStatusCode() == 200) {
            this.result = response.getBody();
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            animals = (List<Object>) results.get('animals');
        }
        return null;
    }
        
}
Visual Force Page
<apex:page controller="AnimalsCallouts">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockButtons>
                <apex:commandButton action="{!makeGetCallout}" value="GetCall" reRender="detail"/>
                
            </apex:pageBlockButtons>
            
            <apex:PageBlockSection id="detail">
                 {!result }
                 <apex:pageBlockTable value="{!animals }" var="an">
                     <apex:column value="{!an}"/>
                 </apex:pageBlockTable>
                 
            </apex:PageBlockSection>
        
        </apex:pageBlock>
    
    </apex:form>


</apex:page>

Output will look like below
User-added image

Let us know if this will help you
 
This was selected as the best answer
sony sonusony sonu
THANKYOU AMIT
Emily Johnson 23Emily Johnson 23
@Amit Chaudhary 8

I did something similar in my class but am having a hard time writing a test class to cover the code using the mock response generator. How would you create the test class for this? When I call the make callout method it says "Non static method cannot be referenced from a static context". I've tried creating the page reference, calling controller, creating a record, etc. in the test class and none of that seems to resolve the issue.