Search code examples
pythonjsonamazon-web-servicesamazon-s3amazon-elasticsearch

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

  • 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.