Loop through values from GET to next PUT request

Here is some example code. I’m using Postman Echo with a POST request to emulate your response, with the loop being a GET request with the filesystem being echoed back as a parameter.

image

This is the body I’m working with.

{
"filesystems": [
        {
        "name": "fs1",
            "root_permissions": "775",
            "mountpoint": "/{{ old path }}/fs1"
        },
        {
            "name": "fs2",
            "root_permissions": "775",
            "mountpoint": "/{{ old path }}/fs2"
        },
        {
            "name": "fsn",
            "root_permissions": "775",
            "mountpoint": "/{{ old path }}/fsn"
        }
    ]
}

This is the response that gets echoed back.

{
    "args": {},
    "data": {
        "filesystems": [
            {
                "name": "fs1",
                "root_permissions": "775",
                "mountpoint": "/{{ old path }}/fs1"
            },
            {
                "name": "fs2",
                "root_permissions": "775",
                "mountpoint": "/{{ old path }}/fs2"
            },
            {
                "name": "fsn",
                "root_permissions": "775",
                "mountpoint": "/{{ old path }}/fsn"
            }
        ]
    },
    "files": {},
    "form": {},
    "headers": {
        "x-forwarded-proto": "https",
        "x-forwarded-port": "443",
        "host": "postman-echo.com",
        "x-amzn-trace-id": "Root=1-63c151b5-75fc92ce3eb9423c73f48789",
        "content-length": "426",
        "content-type": "application/json",
        "user-agent": "PostmanRuntime/7.30.0",
        "accept": "*/*",
        "cache-control": "no-cache",
        "postman-token": "adbb7f76-57c9-498c-84b2-6bc49539e7df",
        "accept-encoding": "gzip, deflate, br"
    },
    "json": {
        "filesystems": [
            {
                "name": "fs1",
                "root_permissions": "775",
                "mountpoint": "/{{ old path }}/fs1"
            },
            {
                "name": "fs2",
                "root_permissions": "775",
                "mountpoint": "/{{ old path }}/fs2"
            },
            {
                "name": "fsn",
                "root_permissions": "775",
                "mountpoint": "/{{ old path }}/fsn"
            }
        ]
    },
    "url": "https://postman-echo.com/post"
}

This is the Tests tab on the first POST request.

// Step 1: Status code check

pm.test("Status code is 200", () => {
  pm.response.to.have.status(200);
});

// Step 2:  Parse the response.

const response = pm.response.json().data;

// Step 3: Create an array of the file system names within the response

let fs = response.filesystems.map(name => name.name); // ["fs1","fs2","fsn"]

// Step 4: Store the array as a collection variable.  Remember to stringify.

pm.collectionVariables.set("fileSystems", JSON.stringify(fs));

This is the pre-request script for the request that we want to loop. (Imaginatively called Loop).

// Step 1: Retrieve file systems array

var array = JSON.parse(pm.collectionVariables.get("fileSystems"));

// Step 2: Retrieve first value in array and reduce array

var currentFileSystem = array.shift();

// Step 3: Set fileSystem variable which is set as a paremeter for this request

pm.collectionVariables.set("fileSystem", currentFileSystem);

// Step 4: Save updated array back to the fileSystems collection variable

pm.collectionVariables.set("fileSystems", JSON.stringify(array));

This is the Tests tab.

// Step 1: Retrieve fileSystems array

var array = JSON.parse(pm.collectionVariables.get("fileSystems"));

// Step 2: Retrieve current filesystem to be used in assertion

var expectedResponse = pm.collectionVariables.get("fileSystem");

// Step 3: Retrieve actual name to be used in assertion
var actualResponse = pm.response.json().args.filesystem;

// Step 4:  Test

pm.test(`Response filesystem = ${expectedResponse}`, () => {
    pm.expect(actualResponse).to.eql(expectedResponse); 
});

if (array.length > 0){
postman.setNextRequest("Loop");

} else {
postman.setNextRequest(null);
}

Finally, this is the Collection Runner results and Console log.

image