Update: Evidently using new Date().toISOString()
works just fine and avoids all this moment lib nonsense. Much <3 to Josh & Liv for the update to my brain.
You're using Postman, like you do, to test your API endpoints before you get into the code. You're going through the POST request and building that JSON when you get to the part where it wants a timestamp in UTC. Hmm, alright, well you probably don't want to have to create a fresh timestamp every time.
Enter JavaScript and Postman's excellent choice to give us access to the Moment library which you can read more about at www.momentjs.com.
For this particular request we need two things: the current UTC timestamp, and a later UTC timestamp (7 days later, for this example). First we'll import the library and then we'll use it, setting two environment variables using the built-in pm.environment.set
function.
var moment = require('moment');
pm.environment.set("now", moment.utc().toJSON());
pm.environment.set("later", moment.utc().add(7, 'days').toJSON());
That's all there is to it! Drop that into the pre-request script area of your request and that'll run before the request is submitted to the endpoint.
To make use of the timestamps, embed them where appropriate in your JSON request with double curly-braces around them.
{
"nodes": [1, 2, 3, 4],
"unmanageFrom": "{{now}}",
"unmanageUntil": "{{later}}",
"relativeTime": false
}
If you're using a relatively recent version of Postman, the variable will turn orange even though they're inside a string. if they're red, you've either misspelled them in your JavaScript or in your request. Give it a closer look.
Top comments (2)
To generate current timestamp, you can also use
{{$isoTimestamp}}
in Postman.Here you can find Pre-request Script area (in my example timespan in milliseconds):