简体   繁体   English

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

[英]Difference between data and json parameters in python requests package

What is the difference between the data and json parameters in the python Requests package? python Requests包中的data和json参数有什么区别?

It is unclear from the documentation文档中不清楚

Does this code:这段代码是否:

import requests
import json
d = {'a': 1}
response = requests.post(url, data=json.dumps(d))

Note that we convert the dict to JSON here ☝️ !请注意,我们在这里将dict转换为 JSON ☝️!

Do anything different than:做任何不同的事情:

import requests
import json
d = {'a': 1}
response = requests.post(url, json=d)

If so, what?如果是这样,是什么? Does the latter automatically set the content-type in the header to application/json ?后者是否自动将标题中的content-type设置为application/json

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 .为了回答我自己的问题,我上面的两个示例似乎做了同样的事情,并且使用json参数确实将标题中的content-type设置为application/json In my first example above using the data parameter, the content-type in the headers would need to be set manually.在上面使用data参数的第一个示例中,标题中的content-type需要手动设置。

As of 2020 I feel that the requests documentation is more clear about the difference, but I still have created a PR to make it more clear .2020 年,我觉得requests 文档更清楚地说明了区别,但我仍然创建了一个 PR 使其更清楚


PS This does NOT answer the OP question but if the FIRST code would be a bit different: PS 这不回答 OP 问题,但如果第一个代码会有所不同:

import requests
import json
d = {'a': 1}
response = requests.post(url, data=d)

- note that the dict d is NOT converted to JSON string here! - 请注意,这里的dict d不会转换为 JSON 字符串!

And if the second code would be the same (copying it for completeness):如果第二个代码相同(复制它以保持完整性):

import requests
import json
d = {'a': 1}
response = requests.post(url, json=d)

...then the result would be quite a lot different. ……那么结果就会大不相同。

First code would generate a request with content type set to application/x-www-form-urlencoded and the data in this format, so: "a=1"第一个代码将生成一个内容类型设置为application/x-www-form-urlencoded的请求,并且数据采用这种格式,因此: "a=1"

The second code would generate a request with content type set to application/json and in fact the data in this format, so {"a": 1} - a JSON string.第二个代码将生成一个内容类型设置为application/json的请求,实际上是这种格式的数据,所以{"a": 1} - 一个 JSON 字符串。

Talking only from my experience here, but please note that it should be preferred to use the json field with the dict, rather than dumping the dict in the data field.这里仅从我的经验谈,但请注意,最好将json字段与 dict 一起使用,而不是将 dict 转储到data字段中。

Again, talking only from experience, I did not study the code itself, but it seems that the requests library does some more clever json serialization than just json.dumps .再次,仅从经验谈起,我没有研究代码本身,但似乎 requests 库做了一些比json.dumps更聪明的 json 序列化。 When using json.dumps in data field, I have encountered several instances where this resulted in an "value is not a valid dict" error response from the (FastAPI) server.data字段中使用json.dumps时,我遇到了几个实例,其中导致来自(FastAPI)服务器的“值不是有效的字典”错误响应。 Using the json field instead fixed these issues.改用json字段解决了这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM