python Requests包中的data和json参数有什么区别?
从 文档 中不清楚
这段代码:
import requests
import json
d = {'a': 1}
response = requests.post(url, data=json.dumps(d))
请注意,我们在这里将 dict
转换为 JSON ☝️!
做任何不同于:
import requests
import json
d = {'a': 1}
response = requests.post(url, json=d)
如果是这样,什么?后者是否自动将标头中的 content-type
设置为 application/json
?
原文由 user1507844 发布,翻译遵循 CC BY-SA 4.0 许可协议
为了回答我自己的问题,我上面的两个例子似乎做了同样的事情,并且使用
json
参数确实将标头中的 --- 设置为content-type
application/json
.在我上面使用data
参数的第一个示例中,标头中的content-type
需要手动设置。