I am trying to send data from salesforce to external system and have written web service but even after properly encoding every parameter, i am getting argument cannot be null due to which data is not getting sent. Please help me with the possible cause of error.
below is the code snippet:
@future(Callout=true)
public static void makePostCallout2(String first, String last, String em, String conID, String acctID, String utype, String prod, String expDate, String acctOwner, String lmd, String flag, String createdBy, String origin) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('***********************');
request.setMethod('PUT');
request.setHeader('Content-Type', 'application/x-www-form-urlencode');
request.setHeader('Authorization', '*****************');
request.setBody('firstname='+EncodingUtil.urlEncode(first,'UTF-8')+'&lastname='+EncodingUtil.urlEncode(last,'UTF-8')+'&email='+EncodingUtil.urlEncode(em,'UTF-8')+'&contactID='+EncodingUtil.urlEncode(conID,'UTF-8')+'&accountID='+EncodingUtil.urlEncode(acctID,'UTF-8')+'&userType='+EncodingUtil.urlEncode(uType,'UTF-8')+'&product='+EncodingUtil.urlEncode(prod,'UTF-8')+'&expiryDate='+EncodingUtil.urlEncode(expDate,'UTF-8')+'&accountOwner='+EncodingUtil.urlEncode(acctOwner,'UTF-8')+'&lastModifiedDate='+EncodingUtil.urlEncode(lmd,'UTF-8')+'&operation='+EncodingUtil.urlEncode(flag,'UTF-8')+'&createdBy='+EncodingUtil.urlEncode(createdBy,'UTF-8')+'&creationOrigin='+EncodingUtil.urlEncode(origin,'UTF-8'));
HttpResponse response = http.send(request);
system.debug('this is after processing response');
// Parse the JSON response
if (response.getStatusCode() != 200) {
System.debug('The status code returned was not expected: ' +response.getStatusCode() + ' ' + response.getStatus());
} else {
System.debug(response.getBody());
}
}
the argument cannot be null is occuring in initializing end point and method in headers.. for privacy reason, endpoint and token has been masked.
request.setBody
? If so, you should implement some sanity checks on your parameters before trying to encode them, or introduce error handling.