如何在Python中为Kartra API格式化有效负载



因此,我试图使用Python requests.post对这个Kartra Inbound API进行API后调用。以下是我的Python代码:

payload = { 
'api_key': settings.KARTRA_API_KEY,
'api_password': settings.KARTRA_API_PASSWORD,
'app_id': settings.KARTRA_APP_ID,
'lead': {
'email': 'joe@joe.com',
'first_name': 'Joseph',
'last_name': 'Smith'
},
'actions': {
'0': {'cmd': 'search_lead'}
}
}
url = settings.KARTRA_API_URL
response = requests.post(url, data=payload)

我必须遵循的唯一例子是PHP(我还没有使用过(。它是这样的:

curl_setopt($ch, CURLOPT_POSTFIELDS,
http_build_query(
array(
'app_id' => 'AIm863DwsOW',
'api_key' => 'QG9GPLW8G',
'api_password' => 'kdwFAfwrfVS',
'lead' => array(
'id' => '3232323223', //you may pass either ID or EMAIL. If both, the system will pick ID
'email' => 'JoeSmith@domain.com',     
),
'actions' => array(
'0' => array(
'cmd' => 'search_lead',
),
)
)

));

我只能得到这样的错误:

{"status":"Error","message":"'actions' not an array","type":"224"}

我已经验证了我的密钥和令牌是否正常工作。

我已经尝试了我能想到的一切,以使端点将"操作"解释为数组。有人能帮我澄清一下吗??非常感谢,因为我浪费了几个小时来处理这个问题。

正如我在评论中所说,Kartra的支持于事无补。Soo。。。我以PHP为例,将它发送到https://httpbin.org/post以了解实际发送的内容。这本身就是一个启示(从来都不知道这样的工具(。

以下是PHP示例的链接:https://documentation.kartra.com/section-28-php-sample-creating-a-lead/

以下是实际发送到api的内容:

{
"args": {}, 
"data": "", 
"files": {}, 
"form": {
"actions[0][cmd]": "create_lead", 
"actions[1][cmd]": "assign_tag", 
"actions[1][tag_name]": "My customer", 
"api_key": "QG9GPLW8G", 
"api_password": "kdwFAfwrfVS", 
"app_id": "AIm863DwsOW", 
"lead[custom_fields][0][field_identifier]": "text1", 
"lead[custom_fields][0][field_value]": "text message", 
"lead[custom_fields][1][field_identifier]": "dropdown1", 
"lead[custom_fields][1][field_value]": "612", 
"lead[custom_fields][2][field_identifier]": "checkbox1", 
"lead[custom_fields][2][field_value][0]": "620", 
"lead[custom_fields][2][field_value][1]": "621", 
"lead[email]": "JoeSmith@domain.com", 
"lead[first_name]": "Joe", 
"lead[last_name]": "Smith"
}, 
"headers": {
"Accept": "*/*", 
"Content-Length": "678", 
"Content-Type": "application/x-www-form-urlencoded", 
"Host": "httpbin.org", 
"X-Amzn-Trace-Id": "Root=1-5f77604a-7b5fee841e85d78f6b352133"
}, 
"json": null, 
"origin": "104.14.117.45", 
"url": "https://httpbin.org/post"
}

以下是测试的实际工作Python代码:

payload = { 
'api_key': settings.KARTRA_API_KEY,
'api_password': settings.KARTRA_API_PASSWORD,
'app_id': settings.KARTRA_APP_ID,
'lead[email]': 'joe@kartra.com',
'lead[first_name]': 'Joe',
'lead[last_name]': 'Smith',
'actions[0][cmd]': 'create_lead',
}
url = settings.KARTRA_API_URL
response = requests.post(url, data=payload)

我从中了解到的另一点是,requests.post((库不会在dicts的dicts的Python dict的第二级以下进行深度复制。查看我的原始问题了解更多。。。

我真的希望这能帮助人们避免我为了解决这个问题而不得不经历的痛苦。诚然,如果我对Python、HTTP和PHP有更多的了解,并且没有试图避免让PHP测试示例代码,这会更容易。生活和学习!

此PHP代码:

'actions' => array(
'0' => array(
'cmd' => 'search_lead',
),

可以被解释为listdict;翻译";PHP的比特:

'actions': [
{
'cmd': 'search_lead'
}
]

编辑:。如果你尝试:

'actions': [
{
0: {'cmd': 'search_lead'}
}
]

如果错误像'actions' not an array一样简单,那么这只能意味着一件事——actions aren't given as an array。在Python中,数组由[]方括号括起来,大括号{}用于dictionary数据结构。

简单地将动作部分更改为数组就可以解决问题

'actions': [
{
0: {'cmd': 'search_lead'}
}
]

这里详细的API还解释了错误的原因。

最新更新