[英]How can I check request body has a key or not in postman tests
I'm trying to write postman tests.我正在尝试编写 postman 测试。 I want to check response according to request so I want to know what requestBody has.
我想根据请求检查响应,所以我想知道 requestBody 有什么。 requestbody is a json like
requestbody 是 json 之类的
{"subscription":{"supi":"value","gpsi":"value","pei":"value"}}.
This 3 elements(supi,gpsi,pei) is optional so I want to check which one exist in requestBody.这 3 个元素(supi、gpsi、pei)是可选的,所以我想检查 requestBody 中存在哪一个。 I can access supi's value if it is exist with below code.
如果以下代码存在,我可以访问 supi 的值。
requestBody = JSON.parse(pm.request.body); var subscription= requestBody.subscription; var supi=subscription.supi;
I try to control if supi exists or not with if(subscription.supi !== null)
.我尝试使用
if(subscription.supi !== null)
控制 supi 是否存在。 As I understand when supi is not in requestBody, subscription.supi does not return null so my if check doesnt work properly.据我了解,当 supi 不在 requestBody 中时,subscription.supi 不会返回 null 所以我的 if 检查无法正常工作。 How can I control supi is exist or not in requestBody?
如何控制 supi 在 requestBody 中是否存在?
In javascript if a object property is not present it will return undefined在 javascript 中,如果 object 属性不存在,它将返回 undefined
so use所以用
if(subscription.supi !== undefined).
Or you can also use或者你也可以使用
Object.keys(subscription).includes(supi)
This will extract all keys and check it has the key supi这将提取所有密钥并检查它是否具有密钥 supi
You can also use你也可以使用
subscription.hasOwnProperty('supi')
This will check the subscription has the property supi and this is the best way这将检查订阅是否具有属性 supi,这是最好的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.