我正在尝试对此API endpoint to add a contact.
进行测试该端点没有示例JSON,对我来说,此描述不清晰:
list_ids - array[string] -The string for the contact list you want to add this contact to. See the POST or GET all lists API - optional
contacts - array[object] - The ID for the contact list where you want to UPSERT the contact. Use GET Contacts method to see Contact List IDs.
这是我正在尝试的JSON,但返回时无效。此端点的有效JSON格式是什么?我需要在联系人数组中添加列表ID吗?
{
"list_ids": [
"a-list-id"
],
"contacts": [
{
"email": "test@example.com"
}
]
}
以上内容始终返回:
{
"errors": [
{
"field": "",
"message": "invalid JSON"
}
]
}
答案 0 :(得分:2)
我知道了:Sendgrid的示例代码是错误的,并且使用其API文档的“试用”选项卡,您会收到关于JSON无效的错误消息。
在此处使用curl示例:
curl --request PUT \
--url https://api.sendgrid.com/v3/marketing/contacts \
--header 'authorization: Bearer <<YOUR_API_KEY>>' \
--data '{"list_ids":["string"],"contacts":[{"address_line_1":"string (optional)","address_line_2":"string (optional)","alternate_emails":["string"],"city":"string (optional)","country":"string (optional)","email":"string (required)","first_name":"string (optional)","id":"string (optional)","last_name":"string (optional)","postal_code":"string (optional)","state_province_region":"string (optional)","custom_fields":{}}]}'
需要添加以下标题:
--header 'content-type: application/json' \
因此完整的请求如下:
curl --request PUT \
--url https://api.sendgrid.com/v3/marketing/contacts \
--header 'authorization: Bearer <<YOUR_API_KEY>>' \
--header 'content-type: application/json' \
--data '{"list_ids":["string"],"contacts":[{"address_line_1":"string (optional)","address_line_2":"string (optional)","alternate_emails":["string"],"city":"string (optional)","country":"string (optional)","email":"string (required)","first_name":"string (optional)","id":"string (optional)","last_name":"string (optional)","postal_code":"string (optional)","state_province_region":"string (optional)","custom_fields":{}}]}'
同样的情况适用于PHP curl示例:
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => "{\"list_ids\":[\"$thing\"],\"contacts\":[{\"email\":\"testphp2@example.com\",\"first_name\":\"string (optional)\",\"last_name\":\"string (optional)\"}]}",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer <<API_KEY>>",
"content-type: application/json"
),
));