使用Python请求加载JSON错误类型

时间:2015-11-20 09:10:28

标签: python json python-requests marklogic

我正在尝试使用Python将一些数据加载到ML。这样可以正常但是类型设置为'T'而不是ML中的'J'。 我想解决这个问题。标题设置似乎只是为了显示所以我该怎么做?

# 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) 

1 个答案:

答案 0 :(得分:3)

如果您使用json参数,requests会为您序列化,因此您不需要自己json.dumps

它还会为您设置内容类型;您可以删除headers关键字参数。

r = requests.post(url, json=data, auth=HTTPDigestAuth('plantje', 'password'))

根据requests documentation

  

您可以直接传递它,而不是自己编码   使用json参数(在版本2.4.2中添加),它将是   自动编码: