0

im trying to test API using loop query parameter tools but the issue is that the query paramas is getting only the last value of "mark", what i need is to run and test every mark and render it into url, here my Pre-request Script enter image description here

and here is the result :

enter image description here

Farouk Mhamdi
  • 311
  • 2
  • 8
  • I'm still struggling to understand what your end goal is. Can you describe the input and output expected. You are looping over a collection of car brands (marks). What do you want to have in the end? Also, you deleted the URL from the postman screenshot but it's on the console log, so you might want to delete that, as the API is public. – bitoiu Oct 06 '22 at 09:53
  • hi @bitoiu , my goal is to test all the brands by entering the brand name in the Query params, here in the pre request script i used for loop to set every car name in the url of GET request,but the issue here as you see the scrpit set only the last brand name "MINI". thank you – Farouk Mhamdi Oct 06 '22 at 10:15

2 Answers2

0

There's several ways to do this but I got it working by using two requests. The first to get the data, the second as the iteration request. You can see the full workflow working on this folder I've created for you. To test it yourself, just fork the collection and run the whole folder on one of your workspaces.

enter image description here

At a high level this is what you do:

  1. Create a folder so you can run these requests as a "flow".
  2. Create one request that gets all "marks", store "marks"
  3. Create another request that:
  • 3.1 While there are marks, remove one mark
  • 3.2 Add mark as a query param
  • 3.3 Save the remaining marks, if none left exit.
  • 3.4 Call the same request again

Code for the first request on Tests:

const responseDate = pm.response.json()
const marks = responseDate.marks.map(x => x.name)

console.log("Obtained a list of brands:", marks)
console.log("Saving brands into marks collection variable")
pm.collectionVariables.set("marks", marks)
postman.setNextRequest("Brand detail");

Code on second request pre-request script:

let marks = pm.collectionVariables.get("marks")
const currentMark = marks.shift()
pm.request.addQueryParams(`marque=${currentMark}`);

if (marks.length > 0) {
    pm.collectionVariables.set("marks", marks)
    postman.setNextRequest("Brand detail");
} else
{
    // Redundant
    postman.setNextRequest(null)
}

You can see on the first one I used your real URL, on the second one I couldn't authenticate so I used the Postman Echo API just to be able to send the request.

NOTE: You have to run the whole folder to chain the requests, otherwise postman.setNextRequest() won't work.

bitoiu
  • 6,893
  • 5
  • 38
  • 60
0
finally i resloved the problem, here's the new script :

let brandnames = pm.collectionVariables.get("brandnames");
 if(!brandnames || brandnames.length == 0) {
    pm.sendRequest("http://cms.ioit.tn/api/cms/home/1", function (err, response) {
            marks = response.json().marks;
            const markname = marks.map(x => x.name);
            let currentbrandname = markname.shift();
            pm.collectionVariables.set("brandname", currentbrandname);
            pm.collectionVariables.set("brandnames", markname);
        });
}
else {
        let currentbrandname = brandnames.shift();
        pm.collectionVariables.set("brandname", currentbrandname);
        pm.collectionVariables.set("brandnames",brandnames);
     }
        if(brandnames==undefined){
            pm.collectionVariables.unset("brandnames");
            }
Farouk Mhamdi
  • 311
  • 2
  • 8