The CreatorCon Call for Content is officially open! Get started here.

POSTMAN API GET - Send multiple GET responses from array values and get return responses.

Ninh V Bui
Kilo Contributor

Hi All, 

I am still quite new to using POSTMAN and also learning javascript so any assistance would be greatly appreciated.

OBJECTIVE:

I have an environment array named CI_IDs which contains multiple CI values that I need to convert to host names using the cmdb_ci table in the ServiceNow REST API. 

Example: When converted through the table query "1ssfy247rh9" = server1

URL example for single CI: https://X.service-now.com/api/now/table/cmdb_ci/106c5c13c61122750194a1e96cfde951 

I have to figure out how to code and get POSTMAN to query each of these values in the array until it runs out. 

Below is my current code:
Pre-request Script:

 

var cis_id = pm.environment.get("CI_IDs");      // variable "cis_id" = environment variable of "CI_IDs"

var current_ci_id = cis_id.shift();                      // Take the first value within cis_id and  pass it on to variable  current_ci_id

pm.environment.set("EVcurrent_ci_id", current_ci_id);   // pass current_ci_id value to an environment variable called "EVcurrent_ci_id".

We then use environment variable EVcurrent_ci_id for the first URL request:

https://X.service-now.com/api/now/table/cmdb_ci/{{EVcurrent_ci_id}}       
 // This one returned back a response body successfully for that single CI. 

find_real_file.png

Now after the first request has ran it will move to the "Tests" tab for post script (Trying to get POSTMAN to run another request here):

find_real_file.png

var cis_id = pm.environment.get("CI_IDs");       // variable "cis_id" = environment variable of "CI_IDs"

// For the below, if cis_id length is higher than 0 run another request with the specified URL (I don't know what to put for the URL)


if (cis_id && cis_id.length > 0) {
postman.setNextRequest("https://X.service-now.com/api/now/table/cmdb_ci/")    // How should I put the request url in this line to make it look into the values within the array???
} else {                                                                                             // If else, cis_id is null, set URL request to null. 
postman.setNextRequest(null);
}

Will this return multiple response bodies? 

Thank you for viewing. 

 

 

1 ACCEPTED SOLUTION

Hi,

So you have list of CI sys-ids in an array and get the host name

You can use the combine array in the query

Once you get the response you can iterate over the json object one by one

https://X.service-now.com/api/now/table/cmdb_ci?sysparm_query=sys_idINCI_IDs

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

5 REPLIES 5

You are welcome

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader