数据必须是一个对象 python 请求库

data must be an object python request library

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"
        }
      }
    }
)

错误: error from the server

在尝试 运行 上面的代码时,我一直收到错误 'additionalInfo must be an object'。有人可以帮我弄清楚为什么。我也尝试传入 json 对象等,但它仍然不起作用。当 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"
        }
      }
    }
)

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