400 Bad Request When Adding Member to MailChimp List

情到浓时终转凉″ 提交于 2020-01-02 19:20:10

问题


I am sending a POST request to the following resource and getting a 400. I understand what the error means, but still am unsure why I'm getting it when a GET request to the same resource works.

/lists/{list_id}/members

Here is a exerpt of the code:

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', // <-- Drop in a GET here and it works, other than it's not the behavior I need.
    env('MAILCHIMP_API_URL') . 'lists/' . env('MAILCHIMP_LIST_KEY') . '/members',
    [
        'auth'  => ['app', env('MAILCHIMP_API_KEY')],
        'query' => [
            'email_address' => 'donnie@test.com',
            'email_type'    => 'html',
            'status'        => 'subscribed',
        ]
    ]);

dd($response->getStatusCode());

Response

Client error: `POST https://XXXX.api.mailchimp.com/3.0/lists/XXXX/members?email_address=donnie%40test.com&email_type=html&status=subscribed`
resulted in a `400 Bad Request`
response: {
  "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.",
  "instance": "f32e7076-b970-4f5c-82c6-eec5875e83b4",
  "errors": [{
    "field": "",
    "message": "Schema describes object, NULL found instead"
  }]
}

回答1:


You are sending a POST request with query parameters. You need to send JSON encoded body!

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', // <-- Drop in a GET here and it works, other than it's not the behavior I need.
    env('MAILCHIMP_API_URL') . 'lists/' . env('MAILCHIMP_LIST_KEY') . '/members',
    [
        'auth'  => ['app', env('MAILCHIMP_API_KEY')],
        'json' => [
            'email_address' => 'donnie@test.com',
            'email_type'    => 'html',
            'status'        => 'subscribed',
        ]
    ]);

dd($response->getStatusCode());


来源:https://stackoverflow.com/questions/46456722/400-bad-request-when-adding-member-to-mailchimp-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!