TypeError:request()获得了意外的关键字参数'json'-PYTHON,AWS

时间:2018-08-30 02:42:19

标签: python json amazon-web-services amazon-s3 amazon-elasticsearch

这里有没有人可以帮助我使用此python脚本。

执行此脚本时出现此错误:

TypeError:request()获得了意外的关键字参数'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)

2 个答案:

答案 0 :(得分:2)

问题在于,默认情况下,Amazon Linux / 2.8.1(可能还有其他版本)安装了request == 1.2.3。根据请求模块的release notes,仅在2.4.2(2014-10-05)中添加了'json'参数

您可以像这样检查安装的版本并升级到最新版本:

$ 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

答案 1 :(得分:0)

仅尝试r = requests.put(url, auth=awsauth, headers=headers)。看起来不需要参数json = payload,因为标头包含该json格式。这里的更多信息(https://github.com/requests/requests/issues/2664)也可能会有所帮助。