How to pass dates into a request with Postman with moment.js


November 19th 2021


It's often handy to pass request parameters dynamically, rather than hard-code them. For example, imagine you're setting up a request to POST to a /appointments endpoint, which expects a appointmentDate to be passed in.

You could set appointmentDate=2021-11-19 in the request body itself, but it might be better to define it relative to the current date so you don't have to go in and modify it next time you run it, to make sure it's not in the past!.

To create an appointmentDate that's always 2 days from now, you can create a pre-request script:

var moment = require('moment');
pm.environment.set('appointmentDate', moment().add(2, 'days')
                                              .format(("YYYY-MM-DD")));

...and then use the {{appointmentDate}} variable in your request.

Hope that helps!

I'm an "old" programmer who has been blogging for almost 20 years now. In 2017, I started Highline Solutions, a consulting company that helps with software architecture and full-stack development. I have two degrees from Carnegie Mellon University, one practical (Information and Decision Systems) and one not so much (Philosophy - thesis here). Pittsburgh, PA is my home where I live with my wife and 3 energetic boys.
I recently released a web app called TechRez, a "better resume for tech". The idea is that instead of sending out the same-old static PDF resume that's jam packed with buzz words and spans multiple pages, you can create a TechRez, which is modern, visual, and interactive. Try it out for free!
Got a Comment?
Comments (0)

 None so far!