简体   繁体   English

如何在 postman 测试中检查请求正文是否有密钥

[英]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.

相关问题 如何在 PostMan 中访问 Request object - How can I access Request object in PostMan 在邮递员中,无论大小写或数组顺序如何,我如何检查响应中的值是否与请求的值相同? - In postman, how can i check if the values in the response is same as that of the request irrespective of case or the order for an array? 如何检查是否按下了某个键? - How can I check that a key has been pressed? 如何检查对象的键上是否有空值? - How can I check if an object has an empty value on a key? 如何使用邮递员测试检查API响应中的一个节点在所有对象下是否具有相同的值? - How to check whether one node in API response has same value under all object using postman tests? 如何使用响应正文作为结果让我的 Postman 测试通过? - How do I get my Postman tests to pass using response body as the results? 邮递员:如何在请求正文中使用环境变量 - Postman: How to use environment variables in Request Body 在Postman中,我如何从“获取请求”中获取响应正文,并将其放入经过细微更改的PUT请求中 - In Postman, how do I take a response body from a Get Request, and put it in a PUT request with minor changes 为什么即使在 Postman 中返回,我也无法从 POST 请求中接收正文? - Why can't I receive the body from a POST request even though it is returned in Postman? 在 postman 测试中,当输入参数可以为空时,如何使用多个值之一断言响应值? - In postman tests, how can i assert the response values with one of multiple values when the input parameter can be empty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM