空手道 - 有没有办法将新的 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?

查看:28
本文介绍了空手道 - 有没有办法将新的 json 键/值对添加到键相同而值不同的现有 JSON 对象中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaScript 和空手道的新手.这本身可能不是一个以空手道为中心的问题,但是,我想知道该解决方案是否可以在空手道本机中完成.

我已经查看了此处的现有问题,但由于我的独特意见,它们似乎不起作用.这个答案看起来很有希望,但对我来说没有用:使用空手道将新的键值对添加到json中

我有一个 JAVA 方法,它为 POST 调用生成一个由 JSON 对象(其中有一个辅助 json 对象)组成的有效负载.有效载荷看起来像这样:

<预><代码>[{keyId":s123",clientId":c0909",区域信息":{地理标签":3s98d238",位置 ID":32}}]

现在我正在做一个测试,我必须在负载中插入一个虚假的键/值对,并确保它在 POST 调用本身中被忽略,我们返回 200.我尝试使用 karate.merge 和 karate.附加,但到目前为止它们还没有工作.

键值对如下所示:

{'bogusfield': 'ABC', 'bogusfield': '123', 'bogusfield': 'abc123', 'bogusfield': 'abc123!$%'}

所以总共会有四个 POST 调用,每个调用都具有与上面不同的值.有没有办法做到这一点?如果我错过了提供任何关键细节/和/或这个问题太新,我深表歉意.在此先感谢您的帮助!

解决方案

这是一个简单的例子,它使用您想要的有效负载编辑发出 4 个请求,这应该会让您顺利进行:

特点:场景大纲:* url 'https://httpbin.org'* 路径任何"* def body = { foo: 'bar' }* body.bogusField = bogusValue* 请求体* 方法贴例子:|bogusValue ||ABC ||123 ||abc123 ||abc123!$% |

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.

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

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
    }
  }
]

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.

The key value pair looks like this:

{'bogusfield': 'ABC', 'bogusfield': '123', 'bogusfield': 'abc123', 'bogusfield': 'abc123!$%'}

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!

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆