代码之家  ›  专栏  ›  技术社区  ›  Donnie

400将成员添加到邮件列表时请求错误

  •  3
  • Donnie  · 技术社区  · 7 年前

    POST 请求到 the following resource 拿到400分。我理解 what the error means ,但仍不确定为什么我在 GET 对相同资源的请求有效。

    /lists/{list_id}/members
    

    $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());
    

    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 回复  |  直到 7 年前
        1
  •  3
  •   Jim Wright    7 年前

    POST 使用请求 query 参数。你需要发送 JSON

    $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());
    
    推荐文章