I need to send data as json with requests module in Python.
For example:
import json
import requests
f = requests.Session()
data = {
"from_date": "{}".format(from_date),
"to_date": "{}".format(to_date),
"Action": "Search"
}
get_data = f.post(URL, json=data, timeout=30, verify=False)
But after run this code, it showed this error:
get_data = f.post(URL, json=data, timeout=30, verify=False) File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 497, in post return self.request('POST', url, data=data, **kwargs) TypeError: request() got an unexpected keyword argument 'json'
I'm on Ubuntu 16.04
and my Python version is 2.7.6
How to issue this problem?
your data is a dict, you should convert it to json format like this:
json.dumps(data)
import json
import requests
f = requests.Session()
headers = {'content-type': 'application/json'}
my_data = {
"from_date": "{}".format(from_date),
"to_date": "{}".format(to_date),
"Action": "Search"
}
get_data = f.post(URL, data=json.dumps(my_data), timeout=30, headers=headers, verify=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With