I can't work out why I'm getting this error. Ultimately, I'm just trying to send a list of SingleEmailMessage via callout, but that's not a valid type for the post method nor can it be directly serialized/deserialized.
RestResource method:
@HttpPost
global static void sendEmails(List<String> emailJSONList) {
//all code is commented out, error still occurs
}
Callout method:
@Future(Callout=true)
public static void sendExceptionEmails(List<String> emailJSONList) {
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('callout:**NamedCredential**/services/apexrest/TestingSomething');
req.setHeader('Content-Type','application/json;charset=UTF-8');
req.setBody('{"emailJSONList":'+String.join(emailJSONList,',')+'}');
System.debug('{"emailJSONList":'+String.join(emailJSONList,',')+'}');
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getBody());
}
Debug (I've replaced the < and > with ^ since it was screwing up the formatting):
10:31:33.9 (11162732)|USER_DEBUG|[38]|DEBUG|{"emailJSONList":[{"bccAddresses":null,"bccSender":false,"ccAddresses":null,"charset":null,"debug":"subject: testsubject0, bccSender: false, saveAsActivity: true, useSignature: true, toAddresses: [test@example.com], htmlBody: ^p^test Body^/p^,", "emailPriority":"Normal","entityAttachments":null,"fileAttachments":null,"htmlBody":"^p^test Body^/p^","inReplyTo":null,"numRecipients":1,"optOutPolicy":"SEND","orgWideEmailAddressId":null,"plainTextBody":null,"references":null,"replyTo":null,"saveAsActivity":true,"senderDisplayName":null,"sentDate":null,"subject":"testsubject0","targetObjectId":null,"templateId":null,"templateName":null,"toAddresses":["test@example.com.com"],"treatBodiesAsTemplate":false,"treatTargetObjectAsRecipient":true,"useSignature":true,"userMail":false,"whatId":null}]}
10:31:33.9 (983471670)|USER_DEBUG|[41]|DEBUG|[{"message":"Illegal value for primitive at [line:1, column:20]","errorCode":"JSON_PARSER_ERROR"}]
There are no parsing issues when I paste it into https://codebeautify.org/jsonviewer.
sendEmails
expects this to be a list, but in the call you pass in a String (because you useString.join(emailJSONList, ',')
. Maybe you should try using a normal Map in the calling function and serialize that into JSON input to the function. – rael_kid Jun 17 '18 at 18:26