function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sony sonusony sonu 

rest callouts

public class Callouts {
    public  static pageReference  makeGetCallout() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('http://www.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
        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());
      
            List<Object> values = (List<Object>) results.get('values');
            System.debug('Received the following values:');
            for (Object value: values) {
                System.debug(value);
            }
        }
        return null;
    }
    public static pageReference makePostCallout() {
    String S ='{"Deduction": [{"Cxatxegoxry_Tyxpe": "xV","xxx": 125.00}],"isOxxnlinexxxxxr": true}';   
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint(' http://xxxxxxxxxxxxxxxxxxxxxxxxxx');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        
           request.setHeader('ip', '2xxxxxxxxxx');
        request.setBody('S');
        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 null;
    }        
}


v.f page is :
<apex:page controller="Callouts" > 
                   <apex:form >
                <apex:pageBlock >
   <apex:pageBlockSection >
       <apex:commandButton value="GET" action="{!makeGetCallout}"/>
       <apex:commandButton value="POST" action="{!makePostCallout}"/>
          </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>

</apex:page>



iam not getting the response on clicking the button may i know where is the mistake?
 
Best Answer chosen by sony sonu
Abdul KhatriAbdul Khatri
OK In the Get, one fix you need to make shown in the screen shot below. Since you are returnning null therefore clicking button will not do anything but you won't get any issue. In the debug log you will see the values

User-added image

 

All Answers

Abdul KhatriAbdul Khatri
I think you are missing the header in the makeGetCallout
 
request.setHeader('Content-Type', 'application/json;charset=UTF-8');

 
sony sonusony sonu
hi abdul;
after keeping that also it is not executing,, i think  , pagereference declaration is worng ...please provide the solution you know
 
Abdul KhatriAbdul Khatri
OK In the Get, one fix you need to make shown in the screen shot below. Since you are returnning null therefore clicking button will not do anything but you won't get any issue. In the debug log you will see the values

User-added image

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
I hope this is a step moving forward.
Abdul KhatriAbdul Khatri
Any updates
Abdul KhatriAbdul Khatri
Hey man what happened you never replied. Was the correction mentioned helpful. 
sony sonusony sonu
hi abdul, the reason , is the endpoint url is in private server , so we had got an error..any way thanks for your reply, iam marking it as a best answer...if possible provide your skype or gmail id, we will have some discussions regarding the above issue