Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailchimp addListMember returns client error 400 bad request when already existing [duplicate]

I'm working on adding a basic Mailchimp subscribe form to a website using their MailchimpMarketing\ApiClient() composer resource. Adding a user seems to work fine, but when trying to add somebody who already exists, i'd expect just a nice json response so that I can catch that error and display it to the user, but instead I get the following GuzzleHttp\Exception\ClientException:

Client error: `POST https://us10.api.mailchimp.com/3.0/lists/xxxxxxxxxx/members` resulted in a `400 Bad Request` response:
{"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/","title":"Member Exists","status" (truncated...)

The documentation doesnt really seem to explain much, I had to find the right way to catch that error from stack overflow too as Mailchimps documentation was.. lacking! Here is the code:

try {
    $response = $mailchimp->lists->addListMember($this->settings_helper->get('mailchimp_list_id'), [
        "email_address" => $form->get_field_value('email'),
        "status" => "subscribed",
        "merge_fields" => [
            "FNAME" => $first_name,
            "LNAME" => $last_name
         ]
    ]);
    
    if ($response->getId()) {
        $this->add_json_success($this->settings_helper->get('mailchimp_success_message'));
    }
} catch (MailchimpMarketing\ApiException $e) {
    $errors[] = $e->getMessage();
} catch (ClientErrorResponseException $e) {
    $errors[] = $e->getMessage();
} catch (GuzzleHttp\Exception\ClientException $e) {
    $errors[] = $e->getMessage();
}

Here is the mailchimp documentation I used: https://mailchimp.com/developer/api/marketing/list-members/add-member-to-list/

I could just catch the error code of 400 and output a custom error but i'm sure that I must be doing soemthing wrong to get such an unhelpful response from the Mailchimp API?

Many thanks, andy and all help is appreciated.

like image 401
Matdragon Avatar asked Sep 03 '20 10:09

Matdragon


2 Answers

I had the same issue, so I ended up directing the error message to see what the whole error was by changing your last catch clause to the following:

catch (GuzzleHttp\Exception\ClientException $e) {
  echo '<pre>' . var_export($e->getResponse()->getBody()->getContents()).'</pre>';
  $errors[] = $e->getMessage();
}

This revealed the following crappy error, which was up to this point truncated:

{
    "type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
    "title":"Invalid Resource",
    "status":400,
    "detail":"[email protected] looks fake or invalid, please enter a real email address.",
    "instance":"fcc1d762-2475-40a6-bc7f-4ac7f3fb7902"
}

So, my issue was that @example.com looks fake or invalid...

Trying a different email, even a @test.com worked. I cannot promise you this will be your issue as well, but checking the error details is definitely a good first step!

like image 103
Ant Avatar answered Nov 15 '22 16:11

Ant


I also struggled with this.

To display the various key values use your own variation on this

catch (\GuzzleHttp\Exception\BadResponseException $e) {
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        $decoded = json_decode($responseBodyAsString);
        echo $decoded->title;
} 
like image 26
Steve Avatar answered Nov 15 '22 16:11

Steve