response = requests.post(
url=bid_url,
headers={'Authorization': my_api_key},
data={
"type": "open",
"initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4",
"dateCreated": datetime.now(),
"subjectId": "8a921487-859f-4931-8743-f69c38f91b25",
"additionalInfo": {
"someInfo": {
"abcd":"123",
"efgh":"456"
}
}
}
)
错误:来自服务器的错误
当尝试运行上面的代码时,我不断收到错误"additionalInfo必须是一个对象"。有人能帮我找出原因吗。我也尝试过传入json对象etc,但仍然不起作用。当additionalInfo字段为空时,它似乎只会给我一个2xx的响应,就像下面的代码一样
response = requests.post(
url=bid_url,
headers={'Authorization': my_api_key},
data={
"type": "open",
"initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4",
"dateCreated": datetime.now(),
"subjectId": "8a921487-859f-4931-8743-f69c38f91b25",
"additionalInfo": {}
}
)
您可以尝试先将datetime.datetime.now()
转换为字符串以避免获得TypeError: Object of type datetime is not JSON serializable
,然后使用json
参数直接传递数据。
查看差异,特别是additionalInfo
数据的情况:
>>> response = requests.post(
... url='https://httpbin.org/post',
... headers={'Authorization': 'abc'},
... data={
... "type": "open",
... "initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4",
... "dateCreated": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
... "subjectId": "8a921487-859f-4931-8743-f69c38f91b25",
... "additionalInfo": {
... "someInfo": {
... "abcd":"123",
... "efgh":"456"
... }
... }
... }
... )
>>> print(response.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"additionalInfo": "someInfo",
"dateCreated": "2021-04-22 09:56:24",
"initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4",
"subjectId": "8a921487-859f-4931-8743-f69c38f91b25",
"type": "open"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": "abc",
"Content-Length": "165",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.25.1",
"X-Amzn-Trace-Id": "Root=1-60812c28-4b724efe3ed5ae33281c1b5e"
},
"json": null,
"origin": "62.216.206.48",
"url": "https://httpbin.org/post"
}
>>> data={
... "type": "open",
... "initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4",
... "dateCreated": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
... "subjectId": "8a921487-859f-4931-8743-f69c38f91b25",
... "additionalInfo": {
... "someInfo": {
... "abcd":"123",
... "efgh":"456"
... }
... }
... }
>>> response = requests.post(
... url='https://httpbin.org/post',
... headers={'Authorization': 'abc'},
... json=data)
>>> print(response.text)
{
"args": {},
"data": "{"type": "open", "initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4", "dateCreated": "2021-04-22 09:57:44", "subjectId": "8a921487-859f-4931-8743-f69c38f91b25", "additionalInfo": {"someInfo": {"abcd": "123", "efgh": "456"}}}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Authorization": "abc",
"Content-Length": "226",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.25.1",
"X-Amzn-Trace-Id": "Root=1-60812c84-58af3b816a5d3fc26cf5f37f"
},
"json": {
"additionalInfo": {
"someInfo": {
"abcd": "123",
"efgh": "456"
}
},
"dateCreated": "2021-04-22 09:57:44",
"initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4",
"subjectId": "8a921487-859f-4931-8743-f69c38f91b25",
"type": "open"
},
"origin": "62.216.206.48",
"url": "https://httpbin.org/post"
}
使用strftime帮助解决了我的问题,而且我不得不将数据类型从响应更改为json。对于未来访问此网站的任何人,请点击此处:
response = requests.post(
url=bid_url,
headers={'Authorization': my_api_key},
json={
"type": "open",
"initiatorId": "ecc52cc1-a3e4-4037-a80f-62d3799645f4",
"dateCreated": datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f'),
"subjectId": "8a921487-859f-4931-8743-f69c38f91b25",
"additionalInfo": {
"BidInfo": {
"abcd":"123",
"efgh":"456"
}
}
}
)
非常感谢所有帮助我的人