我需要为一个看起来像这样的请求编写一个存根:
[
{ "todo_id": 1 },
{ "todo_id": 2 }
]
请求中的 todo 对象的数量可能会有所不同。
我的回复目前如下所示:
"response": {
"status": 200,
"body": "[ {{#each (jsonPath request.body '$') as |todo|}}
{ \"todo_id\": {{todo.todo_id}} },
{{/each}}
]"
}
请注意,我将正文隔开以使其更具可读性,在实际存根中,它都在一行上。
所以我的问题是我的 todo 对象后面需要逗号,以防请求中传递了多个对象。然而,这也给最后一个对象留下了逗号,所以如果发送了上述请求,这将是响应:
[
{ "todo_id": 1 },
{ "todo_id": 2 },
]
最后一个逗号使.json()
Python 应用程序中的方法失败,该应用程序需要从该 WireMock 存根中读取响应。
关于如何摆脱最后一个逗号的任何想法?我在想可能有一个eq
逗号周围的条件并检查当前todo
变量是否相同,{{jsonPath request.body '$.[-1]'}}
但这样写:
{{#eq todo {{jsonPath request.body '$.[-1]'}} }}
也没有工作。
任何有关如何摆脱最后一个逗号的建议将不胜感激。谢谢 :)