I am trying to automate some awkward tasks for work. One of which is query multiple endpoints to get the backup status of an appliance, parse the body response so I just see a datestamp + success/failure result.
I’d like this to work in a 1-click flow but I’m not sure how to compose this in flows.
Below is an example response, I’d like to do the following:
1/ Convert the milisecond timestamp to a DD/MM/YYYY format (or similar, something readable)
2/ Only show me the ‘Cluster_backup_status’ fields.
Can anyone help? I believe I need to write a ‘Test’ to do this, but how can I convert the date/timestamp and output the desired results into a console.
I took @michaelderekjones great example and tweaked it to use the find function and moment to format the dates nicely. Then added error handling in case there were no cluster backups in the response.
This is the beauty of code, there are so many ways of achieving the same result. I prefer the moment library as I find it easier to work with although it’s not really maintained anymore, while it’s built into Postman I’ll continue to use it.
const moment = require('moment')
// retrieve just the cluster_backup_statuses
const results = response.find(r => r.backup_operation_history.cluster_backup_statuses)
const clusterBackupStatus = results ? results.backup_operation_history.cluster_backup_statuses.map(cluster_backup_statuses => cluster_backup_statuses) : undefined
// Update the dates
if (clusterBackupStatus) {
clusterBackupStatus[0].start_time = moment(clusterBackupStatus[0].start_time).format('DD/MM/YYYY');
clusterBackupStatus[0].end_time = moment(clusterBackupStatus[0].end_time).format('DD/MM/YYYY');
// Write updated array to console
console.log(clusterBackupStatus);
} else {
console.log('No cluster backups found')
}
No problem. The more ways of achieving something the better. I’ve only really just started using Postman, coming across from SoapUI.
I need to go back and re-learn Javascript, I know enough to read\edit and understand most examples, but could do with learning the advanced features\methods so I know what is available.
I’m a bit confused - sorry for such newb questions (and this may also evidence my misunderstanding of Postman’s use) - but I had expected to need to write this code in the ‘Tests’ section so it’s ran after the response is received. What does the below do? And where should this be entered in the UI in postman?
As I don’t have direct access to your response, I created a variable that included your sample, for testing the Javascript functions against.
All of this code is in the test tab in PostMan, but I haven’t written any tests per se at this point. I’ve just written to the console log.
Usually I would parse the response using the following command instead if defining a new object.
Something like…
const response = pm.response.json();
This is all covered in the Postman Training. Go to the Postman main page. Select the “Learning Center” down the left hand side. On the next page select Postman training, and I would recommend the “Galaxy API’s 101” and the “Galaxy Testing and Automation” as the starting point. It will show you how to parse the response and write tests.
Postman uses Javascript so it allows you to use a lot of native Javascript functions for manipulation of responses. Searches, date manipulation, etc. So you probably want to take a basic javascript course at some point as well to get the most out of Postman. The course on WC3 schools is a good starting point.