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
and here is the result :
CodePudding user response:
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
At a high level this is what you do:
- Create a folder so you can run these requests as a "flow".
- Create one request that gets all "marks", store "marks"
- 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.
CodePudding user response:
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");
}