Some HTTP requests are long running and instead of asking users to wait for the long running operation to complete, they will return a “request status URI” that, when requested, will return a running/success/failure message.
For example, when you create a new Azure IoT Hub via this request:
https://management.azure.com/subscriptions/:subscriptionId/resourceGroups/:resourceGroupName/providers/Microsoft.Devices/IotHubs/:resourceName?api-version={{api-version}}
It will return an Azure-AsyncOperation header with the following URI:
https://management.azure.com/subscriptions/f9766876-e50b-006f-9ad3-5afb7bb8cf45/resourceGroups/jongtrash/providers/Microsoft.Devices/IotHubs/jongiothubtest2/operationResults/NzRkNDQ3NGUtN2NmNS00YWLWIyZGEtNTMxZjEwYzJjNWI4?api-version=2016-02-03&asyncinfo
Which will have a payload like this when still running:
{
"status": "Running"
}
Or this when the IoT Hub has been created:
{
"status": "Succeeded"
}
Here’s how to deal with this scenario in Postman.
The flow of requests will be executed in this order:
1. Execute the long running request.
2. Execute the check long running request status request.
3. If still running, then delay for 30 seconds and repeat #2. If not still running, then pass or fail the request based on status returned.
Create a “Delay” Request
This will create a Postman timeout that doesn’t chew up your CPU.
URI
https://echo.getpostman.com/delay/{{delay}}
“delay” is the number of minutes to delay before the next execution.
Tests
After the request is delayed it will execute this “test” which checks to see if there’s a “nextRequest” environment variable and then calls the “setNextRequest” method to execute it.
var next = postman.getEnvironmentVariable('nextRequest');
if(next !== ''){
postman.setNextRequest(next);
}
Create Long Running HTTP request
URI
https://management.azure.com/subscriptions/:subscriptionId/resourceGroups/:resourceGroupName/providers/Microsoft.Devices/IotHubs/:resourceName?api-version={{api-version}}
Headers
Authorization:Bearer {{bearerToken}}
Content-Type:application/json
See How to Use Azure Active Directory (AAD) Access Tokens in Postman for instructions on how to get a bearer token.
Body
{
"location": "West US",
"sku": {
"name": "S1",
"tier": "Standard",
"capacity": 1
}
}
Tests
This will first see if the request was successful and then put the Azure-AsyncOperation URI into an Environment Variable (to be executed later). It then sets the “nextRequest” environment variable that will be executed by
var json = JSON.parse(responseBody);
tests["Create or Update IoT Hub"] = !json.error && responseBody !== '' && responseBody !== '{}';
postman.setEnvironmentVariable("createUpdateIoTHubResponseUrl", postman.getResponseHeader("Azure-AsyncOperation"));
postman.setEnvironmentVariable("nextRequest", 'Verify Create or Update IoT Hub');
postman.setNextRequest('Verify Create or Update IoT Hub');
Setup Long Running Status Check Request
This request will call the “Azure-AsyncOperation” URI, check the status, call Delay, which will then execute the status check again.
URI
{{createUpdateIoTHubResponseUrl}}
Headers
Authorization:Bearer {{bearerToken}}
Tests
Add the following test to check the status and if still running will set the next request to ‘Delay’, which in turn will execute this request again.
var test = 'Verify Create or Update IoT Hub';
var json = JSON.parse(responseBody);
if(json.error){
tests[test] = false;
}else{
if(json.status !== 'Running'){
tests[test] = json.status === 'Succeeded';
postman.clearEnvironmentVariable('nextRequest');
}else{
postman.setNextRequest('Delay');
}
}
Execute Requests
When you execute the tests in Collection Running, the output will look like the following:
When you execute in Newman, the requests will look like the following:
Until you get a success message:
Jon