使用 Python 加载 JSON 请求类型错误
Loading JSON using Python Requests wrong type
我正在尝试使用 Python 将一些数据加载到 ML。这工作正常,但类型设置为 'T' 而不是 ML 中的 'J'。
我想解决这个问题。 header 设置似乎只是为了展示,我该怎么做?
# Sending data
data = {'meting': '477', 'bericht': '473', 'plant': '01'}
url = 'http://server:8000/v1/documents?database=thijsPlantjes&extension=json'
headers = {'Content-Type': 'application/json'}
r = requests.post(url, json = json.dumps(data), auth=HTTPDigestAuth('plantje', 'password'), headers = headers)
如果你使用json
参数,requests
会为你序列化,所以你不需要json.dumps
自己。
它还会为您设置内容类型;您可以删除 headers
关键字参数。
r = requests.post(url, json=data, auth=HTTPDigestAuth('plantje', 'password'))
Instead of encoding the dict yourself, you can also pass it directly
using the json
parameter (added in version 2.4.2) and it will be
encoded automatically:
我正在尝试使用 Python 将一些数据加载到 ML。这工作正常,但类型设置为 'T' 而不是 ML 中的 'J'。 我想解决这个问题。 header 设置似乎只是为了展示,我该怎么做?
# Sending data
data = {'meting': '477', 'bericht': '473', 'plant': '01'}
url = 'http://server:8000/v1/documents?database=thijsPlantjes&extension=json'
headers = {'Content-Type': 'application/json'}
r = requests.post(url, json = json.dumps(data), auth=HTTPDigestAuth('plantje', 'password'), headers = headers)
如果你使用json
参数,requests
会为你序列化,所以你不需要json.dumps
自己。
它还会为您设置内容类型;您可以删除 headers
关键字参数。
r = requests.post(url, json=data, auth=HTTPDigestAuth('plantje', 'password'))
Instead of encoding the dict yourself, you can also pass it directly using the
json
parameter (added in version 2.4.2) and it will be encoded automatically: