python - 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
解决方案
假设这是你真正的 python 代码,那么如果你想在 python 中传递多个参数,你应该使用*args
or **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
;做同样的事情。如果它不属于您,请确保您传入正确数量的命名参数。
推荐阅读
- python - 我如何解决opencv python中的“NoneType错误”
- javascript - jQuery追加html元素
- php - 查询完成后无法在 php 文件上发布值
- haskell - Haskell - 如何增加函数中的整数参数?
- git - git log --before="4 个月" 显示从 3 周前提交的分支。我究竟做错了什么?
- c++ - C ++链表下一个指针不移动
- python - 读取和写入 Networkx Graph 的有效方法
- c# - Windows 窗体在未插入时检测按键
- specflow - 并行运行时 SpecFlow+ 目标值不匹配
- intellij-idea - 将 IntelliJ 和 JavaFX 11 与 jdk 11 一起使用时出错