Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 709
user1507844 Avatar asked Nov 01 '14 00:11

user1507844


People also ask

What is a parameter in JSON?

The JSON parameters file provides parameters that you can change to modify the functionality of Log Analysis Tool. Using this file provides you full control over the job without JCL re-generation and ISPF interaction. This section provides more detailed information on the JSON parameter file.

What is request JSON in Python?

json() returns a JSON object of the result (if the result was written in JSON format, if not it raises an error). Python requests are generally used to fetch the content from a particular resource URI. Whenever we make a request to a specified URI through Python, it returns a response object.

What is params data?

Parameters let you interact with user-provided data. For example, you can create calculated fields that include input from people using your report, or pass values back to the SQL query used by your data source. Using parameters makes your reports more interactive.


1 Answers

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.

like image 51
user1507844 Avatar answered Oct 03 '22 15:10

user1507844