Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

request() got an unexpected keyword argument 'json' [duplicate]

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?

like image 616
mySun Avatar asked Oct 28 '17 16:10

mySun


1 Answers

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)
like image 73
DRPK Avatar answered Nov 08 '22 20:11

DRPK