我正在开发一个python脚本,它将与我正在部署的CRM系统的API进行通信。我可以从CRM服务器获得数据,但是我似乎不能添加(写)一个新条目。我怀疑我做了一些愚蠢的事情,因为我对Python和一般编程相当陌生,有人能给我指明正确的方向吗?服务器不拒绝数据,但它的作用就好像我是从/api/v1.0/payments请求数据,而不是发布新数据。
from urllib.request import Request, urlopen
headers = {
'Content-Type': 'application/json',
'X-Auth-App-Key': '[API key]'
}
values = b"""
{
"clientId": 104,
"method": 3,
"checkNumber": "",
"createdDate": "2016-09-12T00:00:00+0000",
"amount": 40,
"note": "",
}
"""
request = Request('http://[SERVER_URL]/api/v1.0/payments', data=values, headers=headers)
response_body = urlopen(request).read()
print(response_body)我是基于API文档中的示例代码工作的:http://docs.ucrm.apiary.io/#reference/payments/payments/post
我在底部正确地使用urlopen吗?
发布于 2017-03-29 13:12:10
This question/answer可能是你的问题。基本上,您的POST请求被重定向到/api/v1.0/ payments / (注意后面的斜杠),当这种情况发生时,您的帖子将被重定向到GET请求,这就是服务器响应的原因,就好像您试图检索所有支付信息一样。
其他要注意的是,json数据实际上是无效的,因为它在' note‘值之后包含了一个尾随的,,因此这也可能是一个问题。我认为您也可能在标题中缺少了Content-Length头。我建议使用json模块来创建json数据:
values = json.dumps({
"clientId": 104,
"method": 3,
"checkNumber": "",
"createdDate": "2016-09-12T00:00:00+0000",
"amount": 40,
"note": ""
})
headers = {
'Content-Type': 'application/json',
'Content-Length': len(values),
'X-Auth-App-Key': '[API key]'
}
request = Request('http://[SERVER_URL]/api/v1.0/payments/', data=values, headers=headers)https://stackoverflow.com/questions/43093505
复制相似问题