[英]Request body has invalid json format - Python
I'm trying to post JSON data (RESTful API) using python. 我正在尝试使用python发布JSON数据(RESTful API)。
null = none
payload = {
"priority": 1,
"hello_id": 207,
"bye_id": 207,
"s1": 1,
"s2": 2,
"sub": "CHECK 123",
"t1": "Leave",
"product_id": null,
"due": "2001-01-01T06:11:54.884Z",
"tags": [
"HelloTag"
]
}
headers = {'content-type': 'application/json'}
r = requests.post(myurl, data=json.dumps(payload), headers=headers)
(OR)
r = requests.post(myurl, json = json.dumps(payload_post), headers=headers)
(OR)
r = requests.post(myurl, data = payload_post, headers=headers, auth=(username_accadmin, password_accadmin))
(OR)
r = requests.post(myurl, json=payload, headers=headers)
None of the above 3 lines seems to yield the expected response (or) the response that I get in Postman. 以上三行代码似乎都没有产生预期的响应(或),我在Postman中得到的响应。
In the response I get :
"Validation failed","errors":[{"field":"priority","message":"Unexpected/invalid field in request","code":"invalid_field"}]
(FOR ALL FIELDS IN THE JSON DATA)
Why is the data wrong even when I convert the dict() to JSON using dumps() method? 为什么即使我使用dumps()方法将dict()转换为JSON,数据也为什么出错?
NOTE : If all the fields in the payload were string, the data is posted as expected. 注意:如果有效负载中的所有字段均为字符串,则数据将按预期发布。
data
should be a dict
or list
, not a string (which the dumps
) returns. data
应该是一个dict
或list
,而不是一个字符串( dumps
返回)。
r = requests.post(myurl, json=payload, headers=headers)
See the documentation . 请参阅文档 。 Also, you should use
None
instead of null
in your payload. 另外,您应该在负载中使用
None
而不是null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.