1
votes

I am trying to build a query filter as an array. So, To make a GET call with some filter in the postman, I'd built an query like:

 "query": [
    {
       "key": "type",
       "value": 3
    },
    {
       "key": "type",
       "value": 4
    },
    {
       "key": "type",
       "value": 5
    }]   

It made the URLs with filter, like

/api/3/vehicles/?type=3&type=4&type=5

But these filters should be getting from previous API call. So, I'd built some script that builds the query like above and save it in the environment variable.

query = []
for (i = 0; i < data.length; i++){
   query.push({'key': 'type', 'value': data[i].id})
}
postman.setEnvironmentVariable("query", query);

And, in the JSON file, I used it like:

"query" : {{query}}

But it seems postman can't recognize it as an environment variable. I can't even import JSON file to the postman. I am getting a format error. Is this something you faced before? How I can solve this problem?

1

1 Answers

0
votes

So when you check the environment variables the "query" variable is not there, right? Also I am not sure about formatting. For declaring environment variable I use: pm.environment.set("query", query);

You can also add console.log(query) after your for loop, open your Postman console(Ctrl+Alt+C) and verify what query looks like. Maybe it will give you a hint.