简体   繁体   中英

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?

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 ☝️ !

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 ?

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.

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 .


PS This does NOT answer the OP question but if the FIRST code would be a bit different:

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!

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"

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.

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.

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 . 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. Using the json field instead fixed these issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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