0

Using a Postman to retrieve data from our project management platform that provides collections (Teamwork)

I retrieve a first list of project ID from the Get request using the following code in the Test of that first Get request :

`var jsonData = JSON.parse(responseBody);

 var list = (jsonData.projects).length;
 var a=[];
 for (var i = 0; i < list; i++) 
              {
               var counter = jsonData.projects[i];
                IDs=counter.id 
                 a.push(IDs)
               }

postman.setEnvironmentVariable("id", a);`

That create a variable id which contains a list of id.

After that, I want to go through each of these id in the following request (replacing {id})

{{Domain}}/projects/{id}/rates.json

Domain is set in the environment variable and is working.

What code and where do i need to put it (Pre-script? Test?) so I can go through the list? That second get request would give me the employee rates in each project (identified by those id)

Thanks for your help

1 Answers1

0

If you want to use the list of variables you extract from the first GET in URLs for subsequent calls, then I think you would need to use the pm.sendRequest option in the 'Test' tab of your first GET.

There is a really good example in this thread: How to run one request from another using Pre-request Script in Postman

Note: The pre-req tab is executed before the API call is made and the test tab is executed after the API call is made.

Also, "postman." is using the old API, you would benefit from using the newer API which is "pm." so for example;

pm.environment.set("variable_key", "variable_value");

More info on this can be found here: https://learning.postman.com/docs/sending-requests/variables/

w4dd325
  • 527
  • 5
  • 21