Python 请求。为布尔值发布 JSON NameError

Python Requests. POSTing JSON NameError for boolean

我正在尝试 POST JSON 使用 Python 和请求 API 的内容,但我得到 NameError 因为我的 JSON 包含一个布尔值。

我试过使用 urllib3,只是请求,两者都显示相同的行为。

"enabled": true,
NameError: name 'true' is not defined

我在 docker 建造:

FROM python:slim

ADD script.py requirements.txt ./

RUN pip install -r requirements.txt

# Run app.py when the container launches
CMD ["python", "script.py"]

requirements.txt

requests==v2.23.0
urllib3==1.25.8

script.py

#import urllib3
import json

resp = req.post('https://example.com/api/endPoint',
  json={
      "name": "myName",
      "rules": [{
        "type": "foo",
        "enabled": true}]
  })

正如@andreis 提到的,尝试在下面的代码片段中使用布尔值 True。 在 python 中,布尔值 true 写为 True

#import urllib3
import json

resp = req.post('https://example.com/api/endPoint',
  json={
      "name": "myName",
      "rules": [{
        "type": "foo",
        "enabled": True}]  # from true to True
  })

https://docs.python.org/3/library/functions.html?highlight=bool#bool