繁体   English   中英

尝试通过 SendGrid 联系人 API 添加新联系人时出现错误 400

[英]Error 400 when trying to add new contact via SendGrid Contacts API

每当我尝试通过 API 添加新联系人时,我都会收到 400 错误。

到目前为止,这是我的代码:

        $emails = json_decode('{
            "list_ids": [
                "LIST_ID_HERE",
            ],
            "contacts": [
                {
                    "email": "example@example.com",
                }
            ]
        }');
        $sendgrid = new SendGrid(getenv('SENDGRID_API_KEY'));
        try {
            $response = $sendgrid->client->marketing()->contacts()->put($emails);;
            print $response->statusCode();
            print_r($response->headers());
            print $response->body();
        } catch (Exception $e) {
            echo 'Caught exception: '. $e->getMessage() ."\n";
        }

我已经尝试过使用和不使用list_ids ,但我得到了相同的响应。 这是此代码中的确切 output :

400Array ( [0] => HTTP/1.1 400 Bad Request [1] => Server: nginx [2] => Date: Sun, 28 Aug 2022 02:41:11 GMT [3] => Content-Type: application/json [4] => Content-Length: 50 [5] => Connection: keep-alive [6] => x-amzn-requestid: 919d1e69-9f8e-46a0-b2d2-72dcca1753c3 [7] => access-control-allow-origin: * [8] => access-control-allow-headers: AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl, X-Recaptcha, X-Request-Source [9] => x-amz-apigw-id: XjZXMHBWPHcF_pQ= [10] => access-control-allow-methods: PUT,OPTIONS,DELETE,OPTIONS [11] => access-control-expose-headers: Link, Location [12] => x-amzn-trace-id: Root=1-630ad5c7-5825916353f578786cb7c908;Sampled=0 [13] => x-envoy-upstream-service-time: 105 [14] => referrer-policy: strict-origin-when-cross-origin [15] => x-content-type-options: nosniff [16] => x-ratelimit-limit: 200 [17] => x-ratelimit-remaining: 199 [18] => x-ratelimit-reset: 49 [19] => [20] => ) {"errors":[{"field":"","message":"invalid JSON"}]}

我假设我做错了什么,但大部分代码是直接从 SendGrid API 文档中复制的。

我会很感激任何帮助,我只是想在我们下周 go 直播之前让它工作。 我不想不得不将此功能排除在生产版本之外。

谢谢!

我认为您的 JSON 无效。 JSON 不处理尾随逗号,因此列表 ID 和 email 地址之后的逗号使此无效。 可悲的是,当给出无效的 JSON 时, json_decode不会引发错误,它只是返回null

尝试这个:

        $emails = json_decode('{
            "list_ids": [
                "LIST_ID_HERE"
            ],
            "contacts": [
                {
                    "email": "example@example.com"
                }
            ]
        }');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM