I have a requirement to call a external service with post method.
When i tested in anymonous block.It's working fine.
But when i try to set up test classes i got this error.
in line
Map results = (Map) JSON.deserializeUntyped(res.getBody());
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.incontact.com/InContactAuthorizationServer/Token');
req.setMethod('POST');
// Specify the required user name and password to access the endpoint
// As well as the header and header information
String username = 'sfintegration';
String password = 'Test';
String vendor = 'Salesforce';
String businessunit ='459';
String application ='Callout';
Blob headerValue = Blob.valueOf(application + '@' + vendor + ':' +
businessunit);
String authorizationHeader = 'BASIC ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
//req.setBody(str);
// Create a new http object to send the request object
// A response object is generated as a result of the request
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('grant_type','password');
gen.writeStringField('username',username);
gen.writeStringField('password',password);
gen.writeStringField('scope','');
gen.writeEndObject();
String jsonS = gen.getAsString();
system.debug('Data in gen'+jsonS);
req.setBody(jsonS);
// Create a new http object to send the request object
// A response object is generated as a result of the request
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
System.debug(res.getStatusCode());
//JSONParser parser = JSON.createParser(res.getBody());
Map<String,Object> results = (Map<String,Object>)
JSON.deserializeUntyped(res.getBody());
//System.debug('Values in results: '+results.values());
//System.debug('Keys in list: '+results.keyset());
accesstoken=String.valueof(results.get('access_token'));
baseurl=String.valueof(results.get('resource_server_base_uri'));
System.debug('Access Token:'+accesstoken);
System.debug('baseurl: '+baseurl);
If(accesstoken !=null && baseurl !=null)
{
for(Lead ts:TriggerLeads)
{
String s=String.valueOf(ts.Phone);
RequestACallback(accesstoken,baseurl,s);
}
}
Please let me know where i missed it.