request() got an unexpected keyword argument 'json'

29,800

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)
Share:
29,800
mySun
Author by

mySun

Loading...

Updated on January 22, 2021

Comments

  • mySun
    mySun over 3 years

    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?