Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an object exist in POSTMAN?

Tags:

json

postman

I want to know how can I test if an object exists. For example, my API return these things :

"data": [     {       "id": 1,       "name": "Abu Dhabi",       "locale": "AE",       "rentWayCountryId": 242,       "stations": [         {           "id": 2,           "rentWayName": "ABU DHABI AIRPORT",           "rentWayStationId": "IAEAUH1",           "bindExtrasToStationToExtraCategory": []         }       ]     }, 

I want to check that data.id exists.

I used the test options in postman and i did this :

var jsonData = JSON.parse(responseBody); tests["Name value OK"] = jsonData.data.id === "1"; 

Could you tell me which condition should I use to verify only if the data exist?

Thank you very much !!

like image 265
Mehdy Driouech Avatar asked May 15 '17 07:05

Mehdy Driouech


People also ask

How do you assert a postman?

In Postman, we can write the assertion in many ways. One of the simplest ways is the snippets, which are nothing but a block of codes that have some unique functions inside it and are available in the postman app. Users can easily access the snippets and can get the code inside the tests editor and run the test.

How do I get a postman's response body?

The Postman Body tab gives you several tools to help you understand the response quickly. You can view the body in one of four views: Pretty, Raw, Preview, and Visualize. in the results pane. You can also place your cursor in the response and select ⌘+F or Ctrl+F.


2 Answers

Here is a proper Postman test:

const jsonData = pm.response.json();  pm.test('Has data', function() {   pm.expect(jsonData).to.have.property('data'); }); 

The above will either PASS or FAIL yor postman request based on the presence of the data property in the response.

like image 162
EricWasTaken Avatar answered Sep 21 '22 09:09

EricWasTaken


To check whether the object is exist or not is equivalent to check whether it is null or not.

if(object){//runs if object is not null} 
like image 33
Abdullah Danyal Avatar answered Sep 23 '22 09:09

Abdullah Danyal