python - python请求包中data和json参数的区别

标签 python json python-requests

python Requests包中的data和json参数有什么区别?

the documentation 不清楚

这个代码:

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)

如果是这样,那是什么?后者是否自动将header中的content-type设置为application/json

最佳答案

要回答我自己的问题,我上面的两个示例似乎做了同样的事情,并且使用 json 参数确实将标题中的 content-type 设置为应用程序/json。在我上面使用 data 参数的第一个示例中,标题中的 content-type 需要手动设置。

关于python - python请求包中data和json参数的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26685248/

相关文章:

python - 如何在Python中进行过滤和分组

python - 当 dict 中的值是一个列表时,如何对 dict 进行这样的分割?

php - JSON 响应字符串以 "null"结尾

javascript - 从本地 (OBJECT) JSON 文件中的数据创建饼图 Highcharts

python - 与 Python 请求一起发送时忽略 URL 参数

Python3 列出来自特定目录的文件

python - 在pandas的分组数据中插入值为0的缺失记录

javascript - 是否可以通过 JSON 接收电子邮件?

Python + SOAP : The message with Action \'\' cannot be processed at the receiver, 由于 EndpointDispatcher 的 ContractFilter 不匹配

python 请求后查询失败 :cookies?