空手道 - 有没有办法将新的 json 键/值对添加到键相同但值不同的现有 JSON 对象? [英] Karate - Is there a way to add a new json key/value pair to an existing JSON object where the key is the same and values are different?
问题描述
我是 JavaScript 和空手道的新手.这本身可能不是一个以空手道为中心的问题,但是,我想知道这个解决方案是否可以在空手道本地完成.
I am a newbie at both JavaScript and Karate. This may not be a Karate centric question per se, however, I am wondering if the solution can be done in Karate natively by any chance.
我已经查看了此处的现有问题,但由于我的独特输入,它们似乎不太可能起作用.这个答案看起来很有希望,但对我来说并不奏效:使用空手道将新的键值对添加到 json 中
I have looked at existing questions on here but they don't seem to work likely due to my unique input. This answer looked promising, but it didn't work out for me: Adding new key-value pair into json using karate
我有一个 JAVA 方法,它为 POST 调用生成一个由 JSON 对象(其中有一个辅助 json 对象)组成的有效负载.有效载荷看起来像这样:
I have a JAVA method that produces a payload consisting of a JSON object (which has a secondary json object in it) for a POST call. The payload looks something like this:
[
{
"keyId": "s123",
"clientId": "c0909",
"regionInfo": {
"geoTag": "3s98d238",
"locationId": 32
}
}
]
现在我正在做一个测试,我必须在有效负载中插入一个虚假的键/值对,并确保它在 POST 调用本身中被忽略,我们返回 200.我尝试使用 karate.merge 和 karate.追加,但到目前为止他们还没有工作.
Now I am doing a test where I have to insert a bogus key/value pair into the payload and make sure it is ignored in the POST call itself and we return a 200. I have tried using karate.merge and karate.append, but they have not worked thus far.
键值对如下所示:
{'bogusfield': 'ABC', 'bogusfield': '123', 'bogusfield': 'abc123', 'bogusfield': 'abc123!$%'}
所以总共会有四个 POST 调用,每个调用的值都与上面不同.有没有办法完成这项工作?如果我错过了提供任何关键细节/和/或如果这个问题太新奇,我深表歉意.提前感谢您的所有帮助!
So in total, there will be four POST calls , each with a different value from above .Is ttherea way to get this done? I apologize if I missed on giving any crucial details/and/or if this too newb of a question. Thank you in advance for all the help!
推荐答案
下面是一个简单的例子,它发出 4 个请求,其中包含您想要的有效负载编辑,这应该会让您顺利上路:
Here's a simple example that makes 4 requests with the payload edits you want which should get you on your way:
Feature:
Scenario Outline:
* url 'https://httpbin.org'
* path 'anything'
* def body = { foo: 'bar' }
* body.bogusField = bogusValue
* request body
* method post
Examples:
| bogusValue |
| ABC |
| 123 |
| abc123 |
| abc123!$% |
这篇关于空手道 - 有没有办法将新的 json 键/值对添加到键相同但值不同的现有 JSON 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!