python请求包中data和json参数的区别 [英] Difference between data and json parameters in python requests package
问题描述
python Requests包中的data和json参数有什么区别?
What is the difference between the data and json parameters in the python Requests package?
是否执行此代码:
import requests
import json
d = {'a': 1}
response = requests.post(url, data=json.dumps(d))
请注意,我们在这里将 dict
转换为 JSON ☝️!
Note that we convert the dict
to JSON here ☝️ !
做任何不同的事情:
import requests
import json
d = {'a': 1}
response = requests.post(url, json=d)
如果是,那是什么?后者是否自动将header中的content-type
设置为application/json
?
If so, what? Does the latter automatically set the content-type
in the header to application/json
?
推荐答案
为了回答我自己的问题,上面的两个示例似乎做了同样的事情,并且使用 json
参数确实设置了content-type
在 application/json
的头文件中.在上面使用 data
参数的第一个示例中,标题中的 content-type
需要手动设置.
To answer my own question, it appears my two examples above do the same thing and that using the json
parameter does indeed set the content-type
in the headers to application/json
. In my first example above using the data
parameter, the content-type
in the headers would need to be set manually.
这篇关于python请求包中data和json参数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!