I searched community and found answers about synchronous executions but none of them suits my needs. In my case repetition number is unknow, depends on response (or timeout). Chaining and nesting is not solution.
I have many PUT requests that executes asyncronously. Request returns “Accepted” and in header “Location” to query execution status. Next I make GET to get status of long running operation. Sleep and make GET again till resource is created (or error).
My current collection looks like:
PUT
Post-response script:
const location = pm.response.headers.get(‘location’);
pm.collectionVariables.set(“LOCATION”, “location”);
GET request queries LOCATION and if status is 206, script waits and reruns GET
GET {{SERVER}}{{LOCATION}}
Pre-request script
var sleepBetweenRetries = 10000;
setTimeout(function() {}, sleepBetweenRetries);
Post-response script
const statusPartialContent = 206;
if(pm.response.code == statusPartialContent) {
pm.execution.setNextRequest(pm.info.requestName);
}
I have a dozen of PUT requests in my collection and affter every PUT one GET. GET is copy/paste of single GET.
I would like to remove GET requests and add logic to PUT Post-response script. Then use pm.sendRequest() in loop, to query status of long running operation.
If status is 206, wait and do GET again. If timeout expired or error returnd from GET, stop procesing. If 201 returned, continue.
As you see, number of iterations is unknown and each GET must run when timeout expires, resource created or error encountered.