4

I collected data from the response and pushed it into array in 'Tests'. console.log shows that I received array:

enter image description here

Then I saved it into environment variable to use in the next call. But value in the request was a string, so only the first value was running.

enter image description here

How can I set normal array?

Response from I collected data:

{
  "sent": 0,
  "remaining": 1000000,
  "gifts": [
    {
        "id": 43468,
        "amount": 50000,
        "can_gift_back": true
    },
    {
        "id": 43469,
        "amount": 50000,
        "can_gift_back": true
    }
  ]
}

My code in the "Tests" tab:

let jsonData = pm.response.json();
let gifts = jsonData.gifts;

//calculate array length
function objectLength(obj) {
    var result = 0;
        for(var prop in obj) {
            if (obj.hasOwnProperty(prop)) {
            result++;
            }    
        }
    return result;
}
let arrayLength = objectLength(gifts);

//push response data to the array
var giftsArray = [];
for (var i = 0; i < arrayLength; i++) {
        var giftsIDs = gifts[i].id;
        giftsArray.push(giftsIDs);
    }
pm.environment.set("giftsToCollect", giftsArray);

UPD:

  1. After using code from the answer in different ways I received such issue.

Point 1 from the picture describes the way of behavior when stringify is used

Point 2 describes behavior when stringify is not usedenter image description here 2. Example of request JSON with manually inputed ids enter image description here

8
  • Have you tried wrapping it in a JSON.stringify(giftsArray) when saving it to the environment variable? Commented Mar 5, 2019 at 17:17
  • @DannyDainton, yes, I tried it in different variants, but it didn't help. Commented Mar 5, 2019 at 19:13
  • What does the saved variable look like, could you update with an image of that? Have you tried removing the quotes and brackets around the variable name in your request body? If it's saved in the environment as an array, all the would need is the {{var_name}} syntax. Commented Mar 5, 2019 at 19:27
  • Saved variable is [5578,5579,5580]. And in the request it is sent that way: gift_ids=[5578,5579,5580,5581,5582,5583,5584,5585,5586,5587] (it's captured request body from Fiddler) instead of every value at different level like at the screen from the my answer. Actually, your code didn't help me... and thanks for a short way to collect IDs :) Commented Mar 5, 2019 at 20:29
  • The code didn't help you? :) Commented Mar 5, 2019 at 20:45

2 Answers 2

4

You could capture all the id values in an array using Lodash, which is an external module that you can use in the Postman application.

Saving the array as a variable after this, is the same as you have done so already but I've added JSON.stringify() around the array value or it will save this as a string.

let giftsArray = []

_.each(pm.response.json().gifts, (item) => {
    giftsArray.push(item.id)
})

pm.environment.set('giftsToCollect', JSON.stringify(giftsArray))

You should then be able to reference the environment variable like this:

gift_ids: {{giftsToCollect}}

I've mocked out the request data locally, just to show you this capturing the values from the data.

Postman

Sign up to request clarification or add additional context in comments.

Comments

0

The working hack is to add pm.sendRequest with the data from the second test to the "Tests" tab of the 1st test. In this case the 2d test does not run on the whole, but gifts are collected. It is not the best decision, but it works.

let jsonData = pm.response.json();

let giftsArray = [];

_.each(pm.response.json().gifts, (item) => {
    giftsArray.push(item.id);
});

pm.sendRequest({
    url: 'http://url/api/gifts/collect',
    method: 'POST',
    header: {
        'Content-type': 'application/json',
        'Host': 'url',
        'accept-encoding': 'gzip, deflate',
        'Connection': 'keep-alive'
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify({'api_token': '48696295110ba1e8f9937820dc9b6626', 'user_id': '100650741100901', 'gift_ids': giftsArray, 'version': '6.0.0'})
    }
}, function (err, res) {
console.log(res);
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.