I am facing a problem which I am not finding any answers. I am trying to call a third party API (which is working). But when I try to deserialize the json receive, nothing is happening. Here is my code. Can anyone see any problems here ?
The first callout method is for another api. THe problem comes from the 2nd one
That is my class calloutViewer controller
public with sharing class RF_Controller {
public String requestEndpoint {get;set;}
public String accountodel {get;set;}
public String createnewtask {get;set;}
public String requestMethod {get;set;}
public String requestBody {get;set;}
public String responseStatus {get;set;}
public Integer responseStatusCode {get;set;}
public String responseBody {get;set;}
public string toMail { get; set;}
public string ccMail { get; set;}
public string repMail { get; set;}
public stackExchangeAPI.responseResource response {get;set;}
public RF_Forum.responseResource2 response2 {get;set;}
public List<stackExchangeAPI.questionResource> deserializedQuestions {get;set;}
public List<RF_Forum.questionResource2> deserializedQuestions2 {get;set;}
public List<SelectOption> getMethodOptions(){
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('GET','GET'));
return options;
}
// RF callout2
public void callout2(){
httpResponse res2 = RFAPI.callout2(requestMethod, requestEndpoint, requestBody);
responseStatus = res2.getStatus();
responseStatusCode = res2.getStatusCode();
responseBody = res2.getBody();
}
public void deserializeQuestions2() {
System.assert( string.isNotBlank(responseBody), 'missing responseBody test');
if(string.isNotBlank(responseBody) ) {
response2 = (RF_Forum.responseResource2)JSON.deserialize(responseBody,RF_Forum.responseResource2.class);
deserializedQuestions2 = response2.items2;
}
}
public class responseResource2 {
public List<questionResource2> items2{get;set;}
}
public class questionResource2 {
public Timestamps timestamps {get;set;}
public Counts counts {get;set;}
public Entity entity {get;set;}
}
public class Timestamps {
public String firstSeen {get;set;}
public String lastSeen {get;set;}
}
public class Entity {
public String id {get;set;}
public String name {get;set;}
public String type_Z {get;set;}
}
public class Counts {
public Integer returned {get;set;}
public Integer total {get;set;}
}
}
That is my RFAPI class who does callout and try to deserialize
public with sharing class RF_Forum {
public static String BASE_URL = 'My api url';
//Method to perform a callout and return an httpResponse
public static httpResponse callout2(String httpMethod, String endpoint, String body){
//Instantiate an httpRequest and set the required attributes
String token = 'My api token';
httpRequest req = new httpRequest();
req.setHeader('X-RFToken', token);
req.setMethod(httpMethod);
req.setEndpoint(endpoint);
req.setTimeout(120000);
httpResponse res2 = new http().send(req);
system.debug(res2.toString());
system.debug(res2.getBody());
return res2;
}
public static responseResource2 deserialize(httpResponse res2){
return (responseResource2)JSON.deserialize(res2.getBody(),responseResource2.class);
}
/********************* RF JSon ***********************/
public class responseResource2 {
public List<questionResource2> items2{get;set;}
}
public class questionResource2 {
public Timestamps timestamps {get;set;}
public Counts counts {get;set;}
public Entity entity {get;set;}
}
public class Timestamps {
public String firstSeen {get;set;}
public String lastSeen {get;set;}
}
public class Entity {
public String id {get;set;}
public String name {get;set;}
public String type_Z {get;set;}
}
public class Counts {
public Integer returned {get;set;}
public Integer total {get;set;}
}
}
That's my Apex to have a visual page
<apex:page id="page" controller="RF_Controller" >
<apex:form id="form" >
<apex:sectionHeader title="AKTARUS" />
<apex:pageMessages id="pageMessages" />
<apex:pageBlock >
<apex:pageBlockButtons >
<!-- RF API -->
<apex:commandButton value="Callout 2" action="{!callout2}" rendered="{!$CurrentPage.parameters.offline!='1'}"/>
<apex:commandButton value="Deserialize2" action="{!deserializeQuestions2}" rendered="{!$CurrentPage.parameters.offline!='1'}"/>
<apex:commandButton value="Callout (offline)" action="{!callout2}" rendered="{!$CurrentPage.parameters.offline=='1'}" />
<apex:commandButton value="Deserialize (offline)" action="{!deserializeQuestions2}" rendered="{!$CurrentPage.parameters.offline=='1'}" /> -
</apex:pageBlockButtons>
<apex:pageBlockSection columns="1" title="HttpRequest" collapsible="false" >
<apex:pageBlockSectionItem >
<apex:outputLabel value="setMethod()" />
<apex:selectList size="1" value="{!requestMethod}" >
<apex:selectOptions value="{!MethodOptions}" />
</apex:selectList>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="setEndpoint()" />
<apex:inputText value="{!requestEndpoint}" style="width:90%" />
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection columns="1" title="HttpResponse" collapsible="false" >
<apex:pageBlockSectionItem >
<apex:outputLabel value="getStatusCode()" />
<apex:outputText value="{!responseStatusCode}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="getStatus()" />
<apex:outputText value="{!responseStatus}" />
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel value="getBody()" />
{!responseBody}
</apex:pageBlockSectionItem>
</apex:pageBlockSection>-
<apex:pageBlockSection columns="1" title="JSON Deserializer" rendered="{!deserializedQuestions2 != null}" collapsible="false" >
<apex:pageBlockTable value="{!deserializedQuestions2}" var="i" >
<apex:column headerValue="Question Id" style="width:75px;" >
<apex:outputText value="{!i.timestamps.firstseen}" />
</apex:column>
<apex:column headerValue="Creation Date" style="width:75px;" >
<apex:outputText value="{!i.timestamps.lastseen}" />
</apex:column>
<apex:column headerValue="View Count" style="width:75px;" >
<apex:outputText value="{!i.entity.id}" />
</apex:column>
<apex:column headerValue="Answer Count" style="width:75px;" >
<apex:outputText value="{!i.entity.name}" />
</apex:column>
<apex:column headerValue="Score" style="width:50px;" >
<apex:outputText value="{!i.entity.type_Z}" />
</apex:column>
<apex:column headerValue="Score" style="width:50px;" >
<apex:outputText value="{!i.counts.returned}" />
</apex:column>
<apex:column headerValue="Score" style="width:50px;" >
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
My JSON looks like that:
{
"data": {
"results": [
{
"entity": {
"id": "ip:186.1xx",
"name": "186.183.1xxxx",
"type": "IpAddress"
},
"timestamps": {
"firstSeen": "2018-08-25T01:01:39.946Z",
"lastSeen": "2019-06-25T20:48:30.833Z"
}
},
{
"entity": {
"id": "ip:179.14.2xx",
"name": "179.14.2.xx",
"type": "IpAddress"
},
"timestamps": {
"firstSeen": "2019-04-05T18:07:40.670Z",
"lastSeen": "2019-06-25T21:29:25.148Z"
}
},