I need to call an API that expects unix timestamps / epoch time values, so I have to use a separate site or tool to convert human-readable values to that format – and then if the call doesn’t return the results I’d expect, then while re-checking all the parameters I have to convert them back to human-readable to make sure the values are all really what I want them to be. Awkward.
Is there any way I can use human-readable timestamps in the call body in a way that they would get converted when the call is placed?
Any of these would be preferable to unix for the human-readable format:
YYYYMMDDHHMMSS
YYYYMMDD HHMMSS
YYYY-MM-DD HH:MM:SS ← favorite
Essentially I’ll be using timestamps to specify a range from which I want records retrieved, so it could be relative (from an hour ago to now) but it could also be any day/time in the past from which records are of interest at the moment – so really, any time stamp.
Example 2024-03-20 13:02:00
My hope is that there’s a way to include that in the call body somewhere (in the format where I can understand what time value I’m looking at without translation) but it can be converted to unix time format when the call is placed since the system I’m calling requires unix time.
What does the current request look like? What’s the format of the request body?
You can create what you need in a pre request script, set that as a variable and then use that value within the request body/url/params/some other part of the request.
I can help you craft this but as I don’t know your context and what you’re currently using it’s tricky to know what to suggest.
Yeah, I know I can do it via separate tools but would like to have it right there in postman (ideally in a spot where I can see the human-readable timestamp in relation to the other parameters while verifying that my request is good to go, as there are a variety of other parameters that can be included in the request).
Here’s something basic that you could use or adapt to your needs.
Add this to the request body:
// using the variable created in the pre-request script
{
"min_save": "{{convertedTimestamp}}"
}
Add this to the pre-request script:
const moment = require("moment");
// the value is hardcoded in the script by this can be made a variable and change in a single place
let friendlyTime = "2024-03-20 13: 02:00";
// converted the human friendly time into a unix timestamp
let convertedTime = moment(friendlyTime).unix();
// using a runtime only variable so it's not stored outside the request execution
pm.variables.set("convertedTimestamp", convertedTime)
In the script, it’s taking the human friendly time and using the .unix() function in the moment lib to convert it. It’s storing that as a temp runtime only variable so it can be used in the request body.
The example request is sent and in the response, you can see that the unix timestamp is being sent in the request body.