【问题标题】:return self.request('POST', url, data=data, **kwargs) TypeError: request() got an unexpected keyword argument 'json'return self.request('POST', url, data=data, **kwargs) TypeError: request() got an unexpected keyword argument 'json'
【发布时间】:2020-04-28 00:10:17
【问题描述】:

我正在尝试为托管在另一台 ubuntu 机器上的服务器创建一个事件。

我将下面的代码保存为 test.py 文件,当我从本地 ubuntu 机器执行它时它工作正常,但是当我从远程 aws ec2 机器执行它时,它会抛出错误。

from irisclient import Irisclient
client = IrisClient(
app='oncall',
key='magic',
api_host='http://192.168.54.101:16649'
)

client.incident(plan='Oncall test', context=
{
  "description": "hello",
  "requester": "joe"
}
               )

错误日志

  File "/usr/lib/python2.7/dist-packages/irisclient/__init__.py", line 52, in incident
    r = self.post(self.url + 'incidents', json=incident_json)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 377, in post
    return self.request('POST', url, data=data, **kwargs)
TypeError: request() got an unexpected keyword argument 'json'

【问题讨论】:

    标签: python python-2.7 api amazon-ec2


    【解决方案1】:

    我认为因为您的上下文数据是一个字典,所以尝试将上下文数据转换为 json,如下所示:

    import json
    from irisclient import Irisclient
    
    client = IrisClient(
        app='oncall',
        key='magic',
        api_host='http://192.168.54.101:16649'
    )
    
    data = {
        "description": "hello",
        "requester": "joe",
    }
    
    client.incident(plan='Oncall test', context=json.dumps(data))
    

    【讨论】:

      【解决方案2】:

      这似乎是您的 irisclient 包中的一个错误。阅读回溯。它说在 irisclient/__init__.py 的第 52 行你正在调用 self.post 这显然是从 requests 模块中调用某个实例...并且您的 irisclient 代码正在传递一个关键字参数 json =... 通过那个电话。

      但 requests 库无法识别此关键字参数 (kwarg)。所以它抛出了 TypeError(来自 .../requests/sessions.py 的第 377 行)。

      也许您需要通过 data= 选项传递 JSON,如下所述:Post JSON using Python Requests

      【讨论】:

        【解决方案3】:

        我刚刚将我的 python 升级到 3.5 版本,它可以在不更改代码的情况下运行

        【讨论】:

          猜你喜欢
          • 2020-02-22
          • 1970-01-01
          • 2018-03-29
          • 2021-06-11
          • 1970-01-01
          • 1970-01-01
          • 2021-01-19
          • 1970-01-01
          • 2016-04-15
          相关资源
          最近更新 更多