'TypeError: request() got an unexpected keyword argument 'json' - PYTHON,AWS

Is there anyone here who can help me with this python script.

When I execute this script I am getting this error:

TypeError: request() got an unexpected keyword argument 'json'

import boto3
import requests
from requests_aws4auth import AWS4Auth

host = 'XXXXX' # include https:// and trailing /
region = 'ap-northeast-1'
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)

# Register repository

headers = {"Content-Type": "application/json"}
path = '_snapshot/XXXXX' # the Elasticsearch API endpoint
url = host + path

payload = {
  "type": "s3",
  "settings": {
    "bucket": "XXXXX",
    "region": "ap-northeast-1",
    "role_arn": "XXXXX"
  }
}



r = requests.put(url, auth=awsauth, json=payload, headers=headers)

print(r.status_code)
print(r.text)


Solution 1:[1]

Try just r = requests.put(url, auth=awsauth, headers=headers). Looks like don't need argument json=payload because headers includes that json format. More information here (https://github.com/requests/requests/issues/2664) may also be helpful.

Solution 2:[2]

The problem is that Amazon Linux/2.8.1 (and probably other releases) have requests==1.2.3 installed by default. According to the release notes for the requests module, the 'json' parameter was only added in 2.4.2 (2014-10-05)

You can check your installed version and upgrade to the latest version like so:

$ pip show requests | grep Version
Version: 1.2.3

$ sudo /usr/local/bin/pip install --upgrade requests

$ pip show requests | grep Version
Version: 2.8.1

Solution 3:[3]

I was trying to do the exact same thing as the question here, and had the same requests=1.2.3 library version issues.

The accepted answer did not work for me, as no surprise, since it doesn t send any payload at all.

What did work was using the 1.2.3 "data" argument and feeding it the json payload object as a string:

...
import json
...
r = requests.put(url, auth=awsauth, data=json.dumps(payload), headers=headers)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Justin Wilson
Solution 2 McNolan
Solution 3 Stefan De Laet