here is my visualforce codepublic class CatFactsDaily {
public String catFact3{get;set;}
public void toGetFacts() {
String requestEndPoint = 'https://catfact.ninja/fact';
Http http=new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(requestEndPoint);
request.setMethod('GET');
HttpResponse response=http.send(request);
if(response.getStatusCode()==200){
Map<String, Object> results=(Map<String, Object>) JSON.deserializeUntyped(response.getBody());
catFact3=String.valueOf(results.get('fact'));
}
else {
ApexPages.Message myMsg=new ApexPages.Message(ApexPages.Severity.ERROR, 'There was an error in reading Data');
ApexPages.addMessage(myMsg);
}
}
}
and this is what i get<apex:page controller="CatFactsDaily" >
<apex:pageBlock title="Cat Fact of the day!!">
<apex:pageBlockSection >
<apex:pageMessages ></apex:pageMessages>
<apex:outputText label="CatFact" value="{!catFact3}"></apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Hi Marjan,The issue is that your toGetFacts method is never called. In order to do that you can add a constructor and call that method. Here is the entire class code. You also may need to define the RemoteSiteSettings for the EndPoint URL
Please don't hesitate to mark this best answer.public class CatFactsDaily {
public String catFact3{ get; set; }
public CatFactsDaily(){
toGetFacts();
}
public void toGetFacts() {
String requestEndPoint = 'https://catfact.ninja/fact';
Http http=new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(requestEndPoint);
request.setMethod('GET');
HttpResponse response=http.send(request);
if(response.getStatusCode()==200){
Map<String, Object> results=(Map<String, Object>) JSON.deserializeUntyped(response.getBody());
catFact3=String.valueOf(results.get('fact'));
}else {
ApexPages.Message myMsg=new ApexPages.Message(ApexPages.Severity.ERROR, 'There was an error in reading Data');
ApexPages.addMessage(myMsg);
}
}
}