首页 > 解决方案 > data 必须是对象 python 请求库

问题描述

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 must be an object”。有人可以帮我弄清楚为什么。我也尝试过传入 json 对象等,但它仍然不起作用。当附加信息字段为空时,它似乎只给我一个 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": {}
    }
)

标签: pythonpython-requestshttp-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"
        }
      }
    }
)

非常感谢所有帮助过我的人


推荐阅读