Creating a list of global variables from response body

I’m creating a collection of tests for a web API, and I’ve run into a wall regarding my variables. I currently have tests set up to create variables that I then import as pre-request scripts into the following request body. For instance:

var data = JSON.parse(responseBody);
pm.globals.set(“pieceId1”, data[0].items[0].pieces[0].id);

This works, and I’m able to increment for a few pieceIds by adding +1, +2, etc. to the end of pieces[0].id. But now I’m faced with a task where there will be potentially hundreds of pieceIds that will need to be imported into the next request. Without hard-coding each and every variable (+1, +2, …) how can I create a list of variables gathered from the response body, which can then be imported into a pre-request script for the next task? There must be some way to loop through the response body do this, but anything even remotely related to this that I’ve found in the community has been over my head. The goal would be to pull pieceId1-pieceId100 out of the response body, and be able to insert each pieceId into the proper place in the next request body using a simplified {{pieceId}} variable that would increment each time it’s called. If anyone could point me in the right direction, I would be extremely appreciative! Thanks in advance!

Hi @awest1. I think I can help. :slight_smile:

Since this is partially a Javascript question, I’ll recommend StackOverflow as a great resource. Here’s a question on iterating through arrays, and it has some very thorough answers:

For your use-case, it sounds like you need to send multiple requests with a {{pieceId}} in each. Is that right?

If that’s the case, you can use Javascript to first loop through your response data, and set a global (or other scope of) variable for each

Test script:

var responseJson = pm.response.json();
var data = responseJson[0].items[0].pieces; // the array we're iterating through

for(var i = 0; i < data.length-1; i++) // loop through the array
    var pieceValue = data[i].id; // the specific value were setting as a global variable
    pm.globals.set('piece'+i,pieceValue); // use i to increment the name of the global variable we're setting

Then, you can use postman.setNextRequest() to loop the request that’s sending each value, until all values have been sent.

Pre-request script:

// check to see if there is a counter run-scoped variable, and if there isn't create one with value=0.
// this is for iterating through the global variables stored, and stopping when there's none left.

// get the current value of the run-scoped variable "counter"
var counter = pm.variables.get('counter');

// if there is a variable called (e.g.) pieceId0, get it's value and set a run-scoped variable
if(pm.globals.has("pieceId" + counter)){
    var pieceIdValue = pm.globals.get("pieceId" + counter)

// increment the counter variable

// store the current value of the Sandbox variable counter, in the run-scoped variable "counter"
// this is so that the counter value persists between loops of the POST request

// if there is another global variable to send, set the next request as this same request (loop it again)
    postman.setNextRequest("POST Array Data")

Finally, you’ll just need a dynamic variable {{pieceIdDynamic}} in the request sending the IDs. When you run this Collection via the Collection Runner, or a monitor, it will grab all needed data and send it automatically with just 2 requests!

I put together a working example of this approach. Check out the following docs page, and click Run in Postman to get a copy. :slight_smile:

Hope that helps!

Also, here’s some docs from the Learning Center that may also be helpful:

1 Like