首页 > 解决方案 > JSON 有效负载 - 带日期

问题描述

我正在探索为我的应用程序构建一个 API,作为开发人员工具的一部分,我可以看到如下所示的有效负载 -

-X POST -H "Content-Type:application/json" -d '{    "action": "DeviceManagementRouter",     "method": "addMaintWindow",     "data": [{"uid": "/zport/dmd/Devices/Server/Microsoft/Windows/10.10.10.10", "durationDays":"1", "durationHours":"00", "durationMinutes":"00", "enabled":"True", "name":"Test", "repeat":"Never", "startDate":"08/15/2018", "startHours":"09", "startMinutes":"50", "startProductionState":"300"     }     ],    "type": "rpc",    "tid": 1}

我看到以下错误 -

{"uuid": "a74b6e27-c9af-402a-acd0-bd9c4254736e", "action": "DeviceManagementRouter", "result": {"msg": "TypeError: addMaintWindow() got an unexpected keyword argument 'startDate'", "type": "exception", "success": false}, "tid": 1, "type": "rpc", "method": "addMaintWindow"}

以下网址中的代码:

https://zenossapiclient.readthedocs.io/en/latest/_modules/zenossapi/routers/devicemanagement.html

标签: pythonjsoncurl

解决方案


假设这是你真正的 python 代码,那么如果你想在 python 中传递多个参数,你应该使用*argsor **kwargs(关键字参数)。对你来说,似乎kwargs更合适。

def addMaintWindow(self, **kwargs): 
""" 
adds a new Maintenance Window 
"""
_name = kwargs["name"]
# _name = kwargs.pop("name", default_value) to be fail-safe and 
# it's more defensive because you popped off the argument 
# so it won't be misused if you pass **kwargs to next function. 

facade = self._getFacade() 
facade.addMaintWindow(**kwargs) 
return DirectResponse.succeed(msg="Maintenance Window %s added successfully." % (_name))

是关于如何使用它的一个很好的答案。一个更通用的在这里。

如果您不熟悉它们,请先阅读一般的。

这应该会让你在这个阶段通过错误,但你需要为你的facade.addMaintWindow;做同样的事情。如果它不属于您,请确保您传入正确数量的命名参数。


推荐阅读