2

I'm trying to find a way to do a Curl request to MailChimps new API v3.0, that will subscribe a user to a given list. Here is what I have thus far:

use warnings;
use WWW::Curl::Easy;
use JSON;

my $apikey = 'xxxx';
my $listid = 'xxxx';
my $email = '[email protected]';
my $endpoint = "https://us6.api.mailchimp.com/3.0/lists";

my $json = JSON::encode_json([
    'email_address' => $email,
    'status'        => 'pending',
    'merge_fields'  => [
        'FNAME'     => "andy",
        'LNAME'     => "newby"
    ]
]);

my $curl = WWW::Curl::Easy->new;

my $url = "$endpoint/$listid/members/" . Digest::MD5::md5(lc($email));

$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, $endpoint);

$curl->setopt(CURLOPT_USERPWD, 'user:' . $apikey);
$curl->setopt(CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$curl->setopt(CURLOPT_TIMEOUT, 10);
$curl->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
$curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
$curl->setopt(CURLOPT_POSTFIELDS, $json);

my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);

# Starts the actual request
my $retcode = $curl->perform;

# Looking at the results...
if ($retcode == 0) {
        print("Transfer went ok\n");
        my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
        print "Received response: $response_body\n";
} else {
        # Error code, type of error, error message
        print "An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n";
}

The documentation is pretty scarce, due to it being a new API. Has anyone had any success with the MailChimp v3 API, for subscribing someone in Perl? (I'm also open to suggestions for command line curl requests... but everything I tried with regards to that, failed with "internal server errors" coming back from MailChimp, which wasn't very helpful)

UPDATE: As suggested below, I enabled verbose, and it now spits out:

  • Hostname was NOT found in DNS cache
  • Trying 184.86.100.251...
  • Connected to us6.api.mailchimp.com (184.86.100.251) port 443 (#0)
  • successfully set certificate verify locations:
  • CAfile: none CApath: /etc/ssl/certs
  • SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
  • Server certificate:
  • subject: C=US; ST=GA; L=Atlanta; O=ROCKET SCIENCE GROUP; OU=Rocket Science Group; CN=*.api.mailchimp.com
  • start date: 2015-09-22 14:39:14 GMT
  • expire date: 2016-09-22 14:39:13 GMT
  • subjectAltName: us6.api.mailchimp.com matched
  • issuer: C=NL; L=Amsterdam; O=Verizon Enterprise Solutions; OU=Cybertrust; CN=Verizon Akamai SureServer CA G14-SHA2
  • SSL certificate verify ok.
  • Server auth using Basic with user 'user'

    PUT /3.0/lists HTTP/1.1 Authorization: Basic xxxx Host: us6.api.mailchimp.com Accept: / Content-Type: application/json Content-Length: 108

  • upload completely sent off: 108 out of 108 bytes < HTTP/1.1 405 Method Not Allowed

  • Server nginx is not blacklisted < Server: nginx < Content-Type: application/problem+json; charset=utf-8 < Content-Length: 253 < X-Request-Id: 5f6ab08f-69e7-4c9b-b22a-91714331d5b7 < Link: https://us6.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json; rel="describedBy" < Allow: GET, POST < Date: Tue, 13 Oct 2015 11:24:32 GMT < Connection: close < Set-Cookie: _AVESTA_ENVIRONMENT=prod; path=/ <
  • Closing connection 0 Transfer went ok Received response: HTTP/1.1 405 Method Not Allowed Server: nginx Content-Type: application/problem+json; charset=utf-8 Content-Length: 253 X-Request-Id: 5f6ab08f-69e7-4c9b-b22a-91714331d5b7 Link: https://us6.api.mailchimp.com/schema/3.0/ProblemDetailDocument.json; rel="describedBy" Allow: GET, POST Date: Tue, 13 Oct 2015 11:24:32 GMT Connection: close Set-Cookie: _AVESTA_ENVIRONMENT=prod; path=/

{"type":"http://kb.mailchimp.com/api/error-docs/405-method-not-allowed","title":"Method Not Allowed","status":405,"detail":"The requested method and resource are not compatible. See the Allow header for this resource's available methods.","instance":""}

I'm not really sure what to make of that though :/

Working code: Thanks to TooMuchPete, I managed to get it going. For anyone who may come across this while trying to use the MailChimp API (3.0) in Perl, below is a working sample (you just need to replace the values of the email, name, api key, and list id);

    use WWW::Curl::Easy;
    use JSON;
    use Digest::MD5;

    my $apikey = 'xxxx-us6';
    my $listid = 'xxxx';
    my $email = '[email protected]';
    my $endpoint = "https://us6.api.mailchimp.com/3.0/lists";

    my $json = JSON::encode_json({
        'email_address' => $email,
        'status'        => 'pending',
        'merge_fields'  => {
            'FNAME'     => "andy",
            'LNAME'     => "newby"
        }
    });

    my $curl = WWW::Curl::Easy->new;

    my $url = "$endpoint/$listid/members/" . Digest::MD5::md5(lc($email));

    $curl->setopt(CURLOPT_HEADER,1);
    $curl->setopt(CURLOPT_URL, $url);

    $curl->setopt(CURLOPT_VERBOSE, 1);
    $curl->setopt(CURLOPT_USERPWD, 'user:' . $apikey);
    $curl->setopt(CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    $curl->setopt(CURLOPT_TIMEOUT, 10);
    $curl->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
    $curl->setopt(CURLOPT_SSL_VERIFYPEER, 0);
    $curl->setopt(CURLOPT_POSTFIELDS, $json);

    # A filehandle, reference to a scalar or reference to a typeglob can be used here.
    my $response_body;
    $curl->setopt(CURLOPT_WRITEDATA,\$response_body);

    # Starts the actual request
    my $retcode = $curl->perform;

    # Looking at the results...
    if ($retcode == 0) {
            print("Transfer went ok\n");
            my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
            # judge result and next action based on $response_code
            print "Received response: $response_body\n";
    } else {
            # Error code, type of error, error message
            print "An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n";
    }

I hope this saves someone the grief I had :)

4
  • Try running $curl->setopt(CURLOPT_VERBOSE, 1); to get the verbose output. It may help you to debug further. Oct 13, 2015 at 11:22
  • @SabujHassan - thanks, I've updated my question above, to include the debug on that (auth key was taken out) Oct 13, 2015 at 11:27
  • 1
    @AndrewNewby I just stopped by to say thank you and I love you. This saved me days worth of work manually creating a solution.
    – ljamison
    Jan 28, 2019 at 21:53
  • @ljamison no worries - I'm glad my pain saved someone else some =) hehe Jan 29, 2019 at 7:40

2 Answers 2

2

You're attempting to connect to $endpoint instead of $url.

my $url = "$endpoint/$listid/members/" . Digest::MD5::md5(lc($email));

$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, $endpoint);

should be:

my $url = "$endpoint/$listid/members/" . Digest::MD5::md5(lc($email));

$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, $url);
4
  • thanks! That was it. I've now got another error coming up, but will dig into that: ,"errors":[{"field":"","message":"Schema describes object, array found instead"}]. Oct 13, 2015 at 15:15
  • 1
    Ah... got it .... was using [] instead of {} in the JSON variable ... seems good now (get an error about the user existing, which is fine, as it does) Oct 13, 2015 at 15:16
  • That's odd -- PUT shouldn't return an error for an existing user, that's the whole point of it, to allow add-or-update functionality. You might want to check with their API support team on that one. Oct 13, 2015 at 16:17
  • yeah, the error was the fact I was using [ ] instead of a hashref: { } ... so it wasn't getting the expected JSON output (as it was being encoded differently) Oct 13, 2015 at 16:54
0

I received an illegal characters response from MailChimp using the code above until I switched to the md5_base64() call.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.