使用Python加载JSON请求的类型错误
我正在尝试使用Python将一些数据加载到ML。这可以正常工作,但类型在ML中设置为“T”,而不是“J”。 我想解决这个问题。标题设置似乎只是为了显示,那么我该怎么做呢使用Python加载JSON请求的类型错误,python,json,python-requests,marklogic,Python,Json,Python Requests,Marklogic,我正在尝试使用Python将一些数据加载到ML。这可以正常工作,但类型在ML中设置为“T”,而不是“J”。 我想解决这个问题。标题设置似乎只是为了显示,那么我该怎么做呢 # Sending data data = {'meting': '477', 'bericht': '473', 'plant': '01'} url = 'http://server:8000/v1/documents?database=thijsPlantjes&extension=json' headers =
# 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
参数,将为您序列化,因此您不需要自己json.dumps
它还将为您设置内容类型;您可以删除标题
关键字参数
r = requests.post(url, json=data, auth=HTTPDigestAuth('plantje', 'password'))
根据:
您也可以直接传递dict,而不是自己编码dict
使用json
参数(在版本2.4.2中添加),它将
自动编码:
json
关键字参数的全部要点是它为您进行json序列化,包括将Content-Type
头设置为application/json
。它还设置Content-Type
头,这里的headers
参数是完全冗余的。@MartijnPieters,谢谢你提供的信息。我相应地更新了答案。