mailchimp-api icon indicating copy to clipboard operation
mailchimp-api copied to clipboard

'marketing_permissions' field always fails validation with "This value should be of type array"

Open AJB99 opened this issue 5 years ago • 1 comments

I'm attempting to POST a subscriber and declare a value for the marketing_permissions field at the same time, but the Mailchimp API response is insisting that the value I'm providing for the marketing_permissions field is not an array.

But it is most certainly an array, as illustrated here:

$params = [
		'email_address' => $email,
		'status' => 'subscribed',
		'merge_fields' => [
			'STATUS' => $status,
			'FNAME' => $first_name,
			'LNAME' => $last_name,
			'JOBTITLE' => $title,
			'COMPANY' => $company,
			'PHONE' => $phone,
			'AGENCY' => $agency,
			'MESSAGE' => $message
		],
		'marketing_permissions' => [
			[
				'marketing_permission_id' => '1234567890',
				'enabled' => true
			]
		]
	];

	$subscriptionResult = $MailChimp->post('lists/1234567890/members', $params);

And the response I'm getting from the MC API is:

Array
(
    [type] => http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
    [title] => Invalid Resource
    [status] => 400
    [detail] => The resource submitted could not be validated. For field-specific details, see the 'errors' array.
    [errors] => Array
        (
            [0] => Array
                (
                    [field] => marketing_permissions
                    [message] => This value should be of type array
                )

        )

)

Anyone have any idea why the MC API won't accept my array as an array?

AJB99 avatar Jun 26 '20 01:06 AJB99

According to the documentation, marketing_permissions should contain an array of objects.

Looks like you are sending an array of arrays.

Edit: Just came across this myself. Here is a nice fix:

$mailchimp->post('lists/1234567890/members', [
    'email_address'         => $email,
    'status'                => 'subscribed',
    'marketing_permissions' => [
        (object)['marketing_permission_id' => '1234567890', 'enabled' => true],
        (object)['marketing_permission_id' => '2345678901', 'enabled' => true],
        (object)['marketing_permission_id' => '3456789012', 'enabled' => true],
    ]
]);

stefantalen avatar Oct 08 '20 10:10 stefantalen