[我发现当我向Mailchimp API提交批处理操作以将成员添加到Mailchimp听众时,操作失败,并显示此错误...
[{\"field\":\"email_address\",\"message\":\"This value should not be blank.\"}]
这很奇怪,因为我的批处理操作指定了电子邮件地址。
ArrayList<MailchimpOperation> operations = new ArrayList<MailchimpOperation>();
for (Contact contact : contacts) {
MailchimpOperation operation = new MailchimpOperation();
operation.method = "POST";
operation.path = "lists/" + audienceid + "/members";
NewMember newMember = new NewMember();
newMember.email_address = contact.email;
MergeFields mf = new MergeFields();
mf.FNAME = contact.firstname;
mf.LNAME = contact.lastname;
newMember.merge_fields = mf;
newMember.status = "subscribed";
operation.body = newMember.toString();
operation.operation_id = "add_batch_" + batchNumber + "_to_mailchimp_audience_" + audienceid;
operations.add(operation);
}
[我在一些堆栈溢出文章中看到该operation.body应该是一个JSON对象。我也尝试过...
ArrayList<MailchimpOperation> operations = new ArrayList<MailchimpOperation>();
for (Contact contact : contacts) {
MailchimpOperation operation = new MailchimpOperation();
operation.method = "POST";
operation.path = "lists/" + audienceid + "/members";
NewMember newMember = new NewMember();
newMember.email_address = contact.email;
MergeFields mf = new MergeFields();
mf.FNAME = contact.firstname;
mf.LNAME = contact.lastname;
newMember.merge_fields = mf;
newMember.status = "subscribed";
operation.body = newMember;
operation.operation_id = "add_batch_" + batchNumber + "_to_mailchimp_audience_" + audienceid;
operations.add(operation);
}
这会产生错误...
Mailchimp error: The resource submitted could not be validated. For field-specific details, see the 'errors' array.,mailchimp error 1 Schema describes string, object found instead,mailchimp error 2 Schema describes string, object found instead
并且如果我像这样显式设置operation.body,则操作成功...
operation.body = "{\"merge_fields\":{\"FNAME\":\"" + contact.firstname + "\",\"LNAME\":\""
+ contact.lastname + "\"}, \"email_address\":\"" + contact.email + "\", \"status\":\"subscribed\"}";
所以为什么我在第一个设置operation.body = newMember.toString()
的方法失败了?
解决方案是不使用.toString(),而是使用此...
operation.body = new ObjectMapper().writeValueAsString(newMember);