ServiceNow REST API Python PUT分配事件报错:请求Payload非有效JSON求助
问题排查与正确代码示例
错误原因
你的代码中使用requests.put()的data参数传递字典数据时,requests会自动将其编码为application/x-www-form-urlencoded格式,但请求头里声明的是Content-Type: application/json,这就导致ServiceNow接收到的请求体不是合法的JSON格式,从而触发错误。
修正后的代码
import requests id_incident = '49akkkkkkkkkkkkkkkk3' url = 'https://PPPPPPPPP.service-now.com/api/now/table/incident/{}'.format(id_incident) user = "OOOOOOO" pwd = "OOOOOOO" # 设置请求头 headers = {"Content-Type":"application/json","Accept":"application/json"} data = {"assigned_to":"12cccccccccccccccccca"} # 使用json参数传递数据,requests会自动将字典序列化为JSON格式 response = requests.put(url, auth=(user, pwd), headers=headers, json=data, timeout=None) res = response.json() print(res)
额外注意事项
- 确认
assigned_to字段的值是当前用户的系统ID,而非用户名或其他标识 - 检查你的ServiceNow账号是否拥有修改事件(incident)分配人的权限
- 验证实例URL、用户名、密码是否正确无误
内容的提问来源于stack exchange,提问作者Davide Tessarollo
相关产品推荐
相关产品推荐

