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

标签 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 = {'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'))

根据requests documentation :

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 - 使用 Python 加载 JSON 请求错误类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822798/

相关文章:

Python QtGui 日历小部件调用按钮

python - 如何在 robobrowser-python 中发起 POST 请求

python-2.7 - 使用 Python 请求 session 时出现 SSL EOF 错误

python - 矢量化以实现性能

线程执行时 Python GUI 没有响应

javascript - 我的 JSON 中的 & 在 Angular View 中呈现为 & ?

javascript - 动态添加下拉菜单并将数据存储在json中

python - 如何使用 Python Requests 库在 post 请求中发送 cookie?

python - 在 OS X 上安装 numpy 时 pip3 找不到 Python.h

java - Jackson 通过特定键将 json 反序列化为不同的子类