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/