gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:47:19 26 4
gpt4 key购买 nike

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com