5

Objective : Click on the button and get the status and statuscode of the REST request.

VF Page Screenshot

enter image description here

This is my VF Page

<apex:page controller="cntrycontroller" >
<apex:form >
<apex:outputText >Enter a 2 digit country  :  </apex:outputText>
<p/>
<apex:inputtext value="{!input_country}" id="id_inptext"/>
<apex:commandButton id="btn" action="{!fetch_data}"  value="Get from Webservice"/>
<p/>
<apex:outputText >Status : {!response_status}</apex:outputText>
<p/>
<apex:outputtext >Status Code : {!response_status_code}</apex:outputtext>


</apex:form>
</apex:page>

Controller Code

public with sharing class cntrycontroller {


    public PageReference fetch_data() {

        HTTPRequest req = new HTTPRequest();
        String url = 'http://services.groupkt.com/country/get/iso2code/'+input_country;
        req.setEndPoint(url);

        HTTP h = new HTTP();
        HTTPResponse resp = h.send(req);
        response_status = resp.getStatus();
        response_status_code = String.ValueOf(resp.getStatusCode());       

        return null;
    }



public String input_country{get;set;}
public String response_status{get;set;}
public String response_status_code{get;set;}

public cntrycontroller(){
   input_country = 'IN';
   }
}

But when I click on the button I am getting the following error.

enter image description here

Can someone tell what is the issue in this ?

UPDATE :

A similar code works well in Anon Apex. The working code is shown below

HTTPRequest req = new HTTPRequest();
String cntry = 'AZ';
//http://services.groupkt.com/country/get/iso2code/IN
String url = 'http://services.groupkt.com/country/get/iso2code/'+cntry;
req.setEndPoint(url);
req.setMethod('GET');

HTTP h = new HTTP();
HTTPResponse resp = h.send(req);
System.debug('Status of the response : '+resp.getStatus());
System.debug('Status code of the response : '+ resp.getStatusCode());
System.debug('Body of the response : '+ resp.getBody());
4

You need to mention your Callout is either HTTP GET or POST via http.setmethod() .As you see in your second code thats mentioned so it works and first you have forgotten so is the exception.

Here is the updated controller code

public with sharing class cntrycontroller {


public PageReference fetch_data() {

    HTTPRequest req = new HTTPRequest();
    String url = 'http://services.groupkt.com/country/get/iso2code/'+input_country;
    req.setEndPoint(url);
    req.setMethod('GET');//DONT FORGET THIS GUY .HE IS IMPORTANT
    HTTP h = new HTTP();
    HTTPResponse resp = h.send(req);
    response_status = resp.getStatus();
    response_status_code = String.ValueOf(resp.getStatusCode());       

    return null;
}

public String input_country{get;set;}
public String response_status{get;set;}
public String response_status_code{get;set;}

 public cntrycontroller(){
  input_country = 'IN';
 }
}

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.