2

In a GET request in Postman, I have added the script to the test section. The get request is run multiple times due to a page limitation. I am not able to collect all the data in one request. For the explanation, I grabbed a part of my code to make it as simple as possible.

In the items variable is an array of id's, which I set via the code below. After that I run the get request again:

pm.collectionVariables.set("array",JSON.stringify(items));
postman.setNextRequest("Get Products");

The result is that each time the Array collectionVariable is overwritten with the new items that I collected. What I want is to add them to the collection variable.

Have tried multiple things, such as:

pm.collectionVariables.set("array",(pm.collectionVariables.get("array") + JSON.stringify(items)));
VB_Dojnaz
  • 259
  • 6
  • 19
Test Test
  • 21
  • 3

1 Answers1

0
pm.collectionVariables.set("array",items);
let arr = pm.collectionVariables.get("array").push(...items)
pm.collectionVariables.set("array",arr);

you can array as array itself , don't need to stringify

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • @TestTest pelase accept the answer by clicking the tick sign if it helped :) – PDHide Jul 16 '21 at 12:24
  • but if I do pm.collectionVariables.set("array",arr); then in my collection variable I have "item1,item2" as string and when I use again the pm.collectionVariables.get("array") it returns a string; – Giox Mar 17 '22 at 12:51
  • @Giox it stores as string only if u use JSON.stringify , else it wil lstore as object itself . – PDHide Mar 19 '22 at 16:59
  • I haven't this behaviour, or at least I'm facing a very strange case where the postman scripts break: https://stackoverflow.com/questions/71513276/postman-set-a-variable-from-an-array-for-each-request Any idea? – Giox Mar 20 '22 at 09:23