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
sksfdc221sksfdc221 

First error: No content to map to Object due to end of input

I have an apex class which fetches and upserts data from external source to Salesforce.

Below is the batch class:
 
global class PolicyCalloutBatchClass implements Database.Batchable<Integer>,Database.AllowsCallouts, Database.Stateful {
 global Iterable<Integer> start(Database.BatchableContext context) {
       Integer[] values = new Integer[0];
while(values.size() < 2999) {
    values.add(values.size());
}
return Test.isRunningTest() ? new Integer[1] : values ;
}
    global void execute(Database.BatchableContext context, Integer[] values) {
        
        HttpRequest policyreq = new HttpRequest();
        policyreq.setMethod('GET');
        policyreq.setTimeout(120000);
        policyreq.setEndpoint('<endpoint>');
        policyreq.setHeader('Authorization', 'Bearer ' + <token>);
        Http policyhttp = new Http();
        HTTPResponse policyres = policyhttp.send(policyreq);
        String policyresponse = policyres.getBody();
        JsonParser objJsonParser = (JsonParser) JSON.deserialize(policyresponse, JsonParser.class);  
        
        JsonParser.cls_value clsValue = objJsonParser.value;
        Map<String, JsonParser.cls_data> clsDataMap = new Map<String, JsonParser.cls_data>();
        for(JsonParser.cls_data objClsData: clsValue.data){
            clsDataMap.put(objClsData.id, objClsData);
        }
        list<Policy__c> updatelist = new list<Policy__c>();
        for (String eachIdFromMap : clsDataMap.keySet()){
            
            Policy__c policy = new Policy__c(
                unique_id__c = clsDataMap.get(eachIdFromMap).id,
                agent_id__c = clsDataMap.get(eachIdFromMap).agentId);
            updatelist.add(policy);  
        }
        try{
            upsert updatelist unique_id__c;    
        }
        catch(DmlException e){
            system.debug('This class didnt compile');
        }
    }
    global void finish(Database.BatchableContext context) {
        
    }
}

Below is my JSON Parser Class:
 
public class JSONParser{
	public Integer code;	//200
	public cls_value value;
	public class cls_value {
		public cls_data[] data;
		public String message;	//Submission Details retrieved successfully.
		public Integer code;	//100040
	}
	public class cls_data {
		public String agentID;	
		public string id;	//24370
	}
	
	
	public static JSONParser parse(String json){
		return (JSONParser) System.JSON.deserialize(json, JSONParser.class);
	}

	
}

Now, when I run the batch class, although the records are getting processed, but I am seeing the error as 
 
First error: No content to map to Object due to end of input

Wondering what's wrong with my code. Can anyone please suggest changes to my code so that I can get this done.
AnudeepAnudeep (Salesforce Developers) 
Based on the error message this appears to be a JsonMappingException 

Per discussion here the problem (most probably) occurs you don't specify the right content type Content-Type: application/json when you POST the request. If you don't explicitly specify it Content-Type: application/x-www-form-urlencoded will be choosen which will lead in your exception.

 
sksfdc221sksfdc221
@Anudeep,

Even though I have given the headers, I could see the same error when the batch job runs