Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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());
share|improve this question
up vote 4 down vote accepted

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';
 }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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