将API请求放入Python返回错误

将API请求放入Python返回错误,python,json,rest,Python,Json,Rest,我对使用API是相当陌生的,所以请耐心听我说。我已经搜索了其他类似的问题,但还没有找到任何解决方案来解决我的问题 通过使用Postman,我能够使用JSON发出Put请求,而且效果很好。当我尝试在Python中使用相同的JSON主体时,我遇到以下错误: {'code': 'E.Internal', 'error': 'An internal error has occurred processing your request. Please notify Customer Support for

我对使用API是相当陌生的,所以请耐心听我说。我已经搜索了其他类似的问题,但还没有找到任何解决方案来解决我的问题

通过使用Postman,我能够使用JSON发出Put请求,而且效果很好。当我尝试在Python中使用相同的JSON主体时,我遇到以下错误:

{'code': 'E.Internal', 'error': 'An internal error has occurred processing your request. Please notify Customer Support for assistance.', 'status': 'error'}
公司的客户支持不是很有帮助,所以我想看看这里是否有人可以帮我

这是我的剧本:

url = 'https://website.com/rest/site/' + record_id

json_body = (JSON body here, same one that works in Postman)
head = {'Accept':'application/json', 'Content-Type': 'application/json'}

response = requests.put(url, auth=(username, password), json=json_body, headers=head)
data = response.json()
print(data)
如果我在auth=username、password之后更改requests.put-to-requests.get并删除所有内容,它会正常工作并返回记录的json,因此我可以连接到API,而不用Python将任何内容放入其中。同样,基本上同样的事情也适用于邮递员


我到底做错了什么?如何输入数据?

根据请求文档,您没有正确填写put函数。像这样试试

import json
import requests

url = 'https://website.com/rest/site/' + record_id

headers = {
    "Content-Type": "application/json",
    'Accept':'application/json'
}

payload = json.dumps({
    "data_goes_here": None
})

rv = requests.put(url, data=payload, headers=headers, verify=False)
if rv.status_code > 399:
    rv.raise_for_status()
print(json.loads(rv.text))

请将您的程序缩减为显示错误的尽可能短的完整程序。尝试使用公开的URL来重现您的错误,例如,http://httpbin.org/put. 当我将您的程序修改为使用httpbin.org/put并填写json_正文行时,我没有收到任何错误。有关为什么以及如何提供可复制测试用例的更多信息,请参见.FYI,这里是我根据您的代码片段构建的示例:我已经很久没有使用请求了,但是JSON主体不应该在数据字段中吗?所以requests.puturl,auth=username,password,data=json\u body,headers=headeshafoc如果你把它作为答案提交,我会选择它。成功了!出于某种原因,我认为它应该是json=,而不是data=。