使用Python调用Pardot API 3执行batchUpsert时JSON格式报错
解决Pardot API 3 batchUpsert接口JSON格式错误问题
问题定位
你的代码存在两个关键问题,导致API返回"Input needs to be valid JSON or XML"错误:
- Headers语法错误:
Pardot-Business-Unit-Id的键值对格式写错,原代码的写法会导致整个headers字典无效,请求发送时头信息异常,API无法正确解析请求体。 - 缺失明确的Content-Type声明:虽然
requests.post的json参数会自动添加Content-Type: application/json,但部分API场景下需要显式声明才能正确识别请求格式。
修正后的代码
import requests data = {"prospects": {"12345": {"website": "https://www.abcd.com"}}} endpoint = "https://pi.pardot.com/api/prospect/version/3/do/batchUpsert" response = requests.post( endpoint, headers={ "Authorization": f"Bearer {access_token}", "Pardot-Business-Unit-Id": business_unit_id, # 修复键值对格式 "Content-Type": "application/json" # 显式声明JSON格式 }, json=data ) # 打印响应信息排查问题 print(response.status_code) print(response.text)
额外验证步骤
- 确认
access_token和business_unit_id变量已正确赋值,且token处于有效期内。 - 检查
prospects下的键是否为目标潜在客户的ID或邮箱地址(与API文档要求一致),确保对应记录存在于Pardot系统中。 - 如果仍报错,可将
data序列化为JSON字符串后用data参数发送,同时手动设置Content-Type:import json response = requests.post( endpoint, headers={ "Authorization": f"Bearer {access_token}", "Pardot-Business-Unit-Id": business_unit_id, "Content-Type": "application/json" }, data=json.dumps(data) )
结果验证
请求成功后,登录Pardot网页端,搜索ID为12345的潜在客户,查看其website字段是否已更新为指定值。
内容的提问来源于stack exchange,提问作者Jan
相关产品推荐
相关产品推荐

