【发布时间】:2023-02-03 11:16:17
【问题描述】:
问题
我查看了一些关于 json 和 data 参数以及它们之间的区别的文档。在我看来,我想我理解其中的区别,最好的解释是 here。
但是,我有一个特定的请求,使用 json 在 PUT 上失败,但使用 data 失败,我不确定为什么。有人可以澄清为什么会这样吗?难道payload里面有list?
语境
我安装了requests==2.28.0。下面是将 PUT 请求提交给事件管理软件 PagerDuty 的 API 的代码,一个使用 data(成功),另一个使用 json(失败)。否则它们是相同的。
奇怪的是their examples使用了json参数。
payload = f'{{"source_incidents": [{{"id": "{child_incident_id}", "type": "incident_reference"}}]}}'
headers = {
'Content-Type': "application/json",
'Accept': "application/vnd.pagerduty+json;version=2",
'From': email,
'Authorization': f"Token token={read_write_api_token}"
}
response = requests.put(f'https://api.pagerduty.com/incidents/{parent_incident_id}/merge', data=payload, headers=headers)
print("response: ", response)
结果:response: <Response [200]>
payload = f'{{"source_incidents": [{{"id": "{child_incident_id}", "type": "incident_reference"}}]}}'
headers = {
'Content-Type': "application/json",
'Accept': "application/vnd.pagerduty+json;version=2",
'From': email,
'Authorization': f"Token token={read_write_api_token}"
}
response = requests.put(f'https://api.pagerduty.com/incidents/{parent_incident_id}/merge', json=payload, headers=headers)
print("response: ", response)
结果:response: <Response [400]>
【问题讨论】:
标签: python json http python-requests