【问题标题】:TypeError: request() got an unexpected keyword argument 'json' - PYTHON,AWSTypeError:request() 得到了一个意外的关键字参数 'json' - PYTHON,AWS
【发布时间】:2019-02-04 21:31:20
【问题描述】:

这里有没有人可以帮助我编写这个 python 脚本。

当我执行这个脚本时,我收到了这个错误:

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)

【问题讨论】:

  • json 不是 put 方法的参数。使用的正确参数是 data
  • 我试过了,但它返回了这个错误:{"Message":"Your request: '/_snapshot/CuratorRepo' is not allowed due to invalid input parameters."}

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


【解决方案1】:

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

【讨论】:

  • 贾斯汀,我试过你的建议,它奏效了。非常感谢
  • 太好了!您能否选择我的复选标记作为答案。谢谢
【解决方案2】:

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

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

$ 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

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2016-09-17
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多