我正在编写一个脚本来从不同的 Web 服务发出请求。从下面的 json 数据发布数据时遇到问题。当我运行 patient_create_bill()
函数时,我从响应中得到以下日志。
RESPONSE IS!!!!!
{'Error': 'JSON parse error - Expecting value: line 1 column 1 (char 0)'}
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): 0.0.0.0:9000
DEBUG:urllib3.connectionpool:http://0.0.0.0:9000 "POST /api/bill/patient/bills/ HTTP/1.1" 400 72
Creating Patient Bill .....................................
我试图在邮递员上发出 POST
我得到 201
响应意味着有效载荷很好,没有任何问题。
这是我的 POST
负载。
我有一个名为 mocks.py
包含 PATIENT_BILL_CREATE_PAYLOAD
PATIENT_BILL_CREATE_PAYLOAD = {
"bill_items": [{
"item": "Syringes",
"qty": 2,
"description": "Medicorp syringes"
}],
"bill_services": [{
"service": "Diagnosis",
"service_type": "1",
"duration": 5,
"description": "diagnosis"
}],
"client": "Sandra Hernandez"
}
这是功能
我已经导入了 PATIENT_BILL_CREATE_PAYLOAD
并在此函数中使用它。
def patient_create_bill(headers):
"""This function uses login creds provided and returns token plus logged in user data."""
url = "http://0.0.0.0:9000/api/bill/patient/bills/"
data = PATIENT_BILL_CREATE_PAYLOAD
res = requests.post(url, data=data, headers=headers)
res_data = res.json()
print("Creating Patient Bill .....................................\n")
return res_data
原文由 Philip Mutua 发布,翻译遵循 CC BY-SA 4.0 许可协议
您自己的答案是正确的(将您的数据编码为 json),这里是固定的代码。这对我有用:
代替
正确的写法是…
请求库文档 中有关此类请求的更多信息。