我正在尝试使用 Python 3 获取 POST 请求,它将向使用基本身份验证的平台提交 json 有效负载。我收到 405 状态错误,并认为这可能是由于我的负载格式造成的。我正在学习Python,但仍然不确定何时使用“与”、对象与数组以及某些请求的语法。搜索时,我无法找到使用基本身份验证发布数组的类似问题。以下是我现在有:
import requests
import json
url = 'https://sampleurl.com'
payload = [{'value': '100','utcRectime': '09/23/2018 11:59:00 PM','comment': "test",'correctionValue': '0.0','unit': 'C'}]
headers = {'content-type': 'application/json'}
r = requests.post(url, auth=('username','password'), data=json.dumps(payload), headers=headers)
print (r)
Run Code Online (Sandbox Code Playgroud)
在 API swagger 中测试,CURL 包含以下格式:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '[{"value": "100","utcRectime": "9/23/2018 11:59:00 PM","comment": "test","correctionValue": "0.0","unit": "C"}]'
Run Code Online (Sandbox Code Playgroud)