Fix Python – Difference between data and json parameters in python requests package

Question

Asked By – user1507844

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?

Now we will see solution for issue: Difference between data and json parameters in python requests package


Answer

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.

This question is answered By – user1507844

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0