Request () received unexpected keyword argument 'json'

I need to send data with json with a request in Python.

Python version: 2.7.6

OS: Ubuntu 16.04

For instance:

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 running this code, show 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'

How to solve this problem?

+4
source share
2 answers

your data is a dict, you have to convert it to json format as follows:

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)
+2
source

Looking here http://docs.python-requests.org/en/master/user/advanced/ I suspect your keyword "json" should be "data"

i.e.

get_data = f.post (URL, data = data, timeout = 30, verify = False)

0
source

Source: https://habr.com/ru/post/1688343/


All Articles