RTFM: Postman learning

In nowadays I work much with different REST services, where generally I need to provide start and end dates in a  request to service. I’m using Postman tool for communication with the services. I love this tool very much, one of the benefits of this tool is the ability to use dynamic scripts written using Javascript language to prepare the required data. Previously I had some issues with expired dates in my requests, so I decided to make them dynamic.

In my cases dates are supposed to be in the format –  YYYY-MM-DD and I usually have such math for them:

  • start date = today + 1 day
  • end date = today + 4 days

I created two global variables in the Postman like startDate and endDate and scripted their initialization using Javascript:

var today = new Date();
var startDay = new Date();
var endDate = new Date();

startDate.setDate( today.getDate() + 1 );
endDate.setDate( today.getDate() + 4 );

pm.globals.set( "startDate", startDate.toISOString().slice(0, 10) );
pm.globals.set( "endDate", endDate.toISOString().slice(0, 10) );

So, the problem was solved pretty fast, but it wasn’t enough for me. I was curious if such a great tool as Postman has something inbound for the date manipulation. I went to the Postman site and started reading the documentation. Spending just a small amount of time on the site my curiosity was rewarded with a prize. The Postman allows using some of the inbound Node.js libraries in custom scripts. One of them is moment – an effective library for the date manipulation.

My problem was solved one more time in another way:

var dateFormat = 'YYYY-MM-DD';
var moment = require('moment');
var startDate = moment().add( 1, day ).format( dateFormat );
var endDate = moment().add( 4, day ).format( dateFormat );

pm.globals.set( "startDate", startDate );
pm.globals.set( "endDate" , endDate );

In my particular case above, both solutions look pretty similar and the usage of the special library doesn’t provide almost any benefits. But in some more complex cases, this might be extremely helpful.

So, the whole point here is: if you think you already know some tool just spend some time to read the documentation and you will be rewarded.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.