更正 JSON Sendgrid 营销活动端点的格式
Correct JSON Format for Sendgrid Marketing Campaign endpoint
我正在尝试测试这个 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"
}
]
}
上面总是returns:
{
"errors": [
{
"field": "",
"message": "invalid JSON"
}
]
}
我想通了: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:
--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"
),
));
我正在尝试测试这个 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"
}
]
}
上面总是returns:
{
"errors": [
{
"field": "",
"message": "invalid JSON"
}
]
}
我想通了: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:
--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"
),
));