My question:
How can I do basic math on a value I’ve put into a variable?
Details:
I’m working with a json response that has a value like this:
“cv”: “637910086836261707^b25e14ec-0a15-e911-83b7-0a419fa6a2a1^01816e3c-be80-44f3-9629-e9ac6ea6e141^0^204.123.456.55”,
That first part is a custom time stamp value. I have accomplished stripping that part out and making a variable of it, thanks to the instructions here: Extract string from response
Now what I need to do is:
- convert this to a number (I assume this would be step 1)
- then do some addition to this value
- and finally set the new value as a collection variable
e.g. timestamp in response above is 637910086836261707. I have put it into a collection variable called “trk_request_ts”. What I would like to do now is basically this…
var new_timestamp = {{trk_request_ts}} + 10000000
THANKS!
I figured it out, will provide my code here in case it helps someone else with the same need in the future.
I added this to the tests section of my first request. Then I can use the variable created in the last line (trk_request_ts_new) in all my future requests.
var response = pm.response.json();
// get the first part of the cv key (the timestamp value) and put it into a collection variable
let cvString = pm.response.json().cv;
pm.collectionVariables.set(“trk_request_ts”, cvString.split(‘^’)[0]);
// log the value
console.log("tracking request timestamp is " + pm.collectionVariables.get(“trk_request_ts”));
// parse string to a number so we can do some arithmetic on it, and log it
var trk_request_ts_int = parseInt(pm.collectionVariables.get(“trk_request_ts”));
console.log("trk_request_ts as a number is " + trk_request_ts_int);
// add 10 million to the timestamp, and log it
var trk_request_ts_new = (trk_request_ts_int + 10000000);
console.log("new timestamp is " + trk_request_ts_new);
// put it in a collection variable for later use
pm.collectionVariables.set(“trk_request_ts_+_1_min”, trk_request_ts_new);