[英]TypeError: request() got an unexpected keyword argument 'json' - PYTHON,AWS
Is there anyone here who can help me with this python script.这里有没有人可以帮助我处理这个 python 脚本。
When I execute this script I am getting this error:当我执行此脚本时出现此错误:
TypeError: request() got an unexpected keyword argument 'json' 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)
The problem is that Amazon Linux/2.8.1 (and probably other releases) have requests==1.2.3 installed by default. 问题在于,默认情况下,Amazon Linux / 2.8.1(可能还有其他版本)安装了request == 1.2.3。 According to the release notes for the requests module, the 'json' parameter was only added in 2.4.2 (2014-10-05) 根据请求模块的发行说明 ,仅在2.4.2(2014-10-05)中添加了“ json”参数
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
Try just r = requests.put(url, auth=awsauth, headers=headers)
. 仅尝试r = requests.put(url, auth=awsauth, headers=headers)
。 Looks like don't need argument json=payload because headers includes that json format. 看起来不需要参数json = payload,因为标头包含该json格式。 More information here ( https://github.com/requests/requests/issues/2664 ) may also be helpful. 此处的更多信息( https://github.com/requests/requests/issues/2664 )可能也会有所帮助。
I was trying to do the exact same thing as the question here, and had the same requests=1.2.3 library version issues.我试图做与这里的问题完全相同的事情,并且有相同的 requests=1.2.3 库版本问题。
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:起作用的是使用 1.2.3“数据”参数并将 json 有效载荷 object 作为字符串提供给它:
...
import json
...
r = requests.put(url, auth=awsauth, data=json.dumps(payload), headers=headers)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.