JEWebDes has asked for the wisdom of the Perl Monks concerning the following question:

I'm far from being a true Perl monk, and here's proof. I've been trying to add a member to a MailChimp list, but keep getting a 400 Bad Request - 'This value should of type object' error. The thing is I don't know how to build the Perl object required. Here's what I've tried:

my $mailchimp = Mail::Chimp3->new( api_key => 'mykey' ); my %merge_fields = ( 'FNAME' => 'My', 'LNAME' => 'Name', 'PHONE' => '760-720-0000' ); my $response = $mailchimp->add_member ( list_id => '49ef108521', email_address => 'test@jmydomain.com', status => 'subscribed', merge_fields => %merge_fields );

Obviously I haven't built the merge_field Perl object correctly. How should it be coded?

Thanks for the help!

Replies are listed 'Best First'.
Re: Mail::Chimp3 Add Member
by Fletch (Bishop) on Jan 27, 2020 at 19:11 UTC

    Not familiar with that module, but my guess is that you should be passing a reference to your hash merge_fields => \%merge_fields insetead. When you name the hash itself there it's expanding out into a list of the key/value pairs in the hash. See perldata, perlref, perldsc.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      Fletch, you are so right. That took care of the error. Thanks.

Re: Mail::Chimp3 Add Member
by 1nickt (Canon) on Jan 27, 2020 at 20:25 UTC

    Hi,

    "object" there refers to a JSON object that the MailChimp server is expecting to receive as the value of the merge_fields param, not a Perl object.

    The module you are using handles converting your arguments to add_member into the right format for the HTTP call, but as Fletch said you are passing in a hash, which expands to a list, so merge_fields is getting as its value just the first key from your hash (and a bunch of other bogus params are being passed and presumably ignored).

    When you pass a reference to the hash instead it is a single data structure and the module will properly convert it to a JSON "object" when making the call to MailChimp.

    Hope this helps!


    The way forward always starts with a minimal test.