I might be losing my mind here, but I've written a Collection-wide pre-request script in Postman for the first time and in order to try and debug it I've put a bunch of console.log() statements in. What I've found is that the value displayed in the console for one of my variables is always the same, even when I'm setting it to something else in the script.
Here's my pre-request script:
// Set Timestamp variable
var moment = require("moment");
var epochTimestamp = moment().unix().toString();
pm.collectionVariables.set("unixTimestamp", epochTimestamp);
// Set ClientIdHash variable
var clientId = pm.environment.get("apiClientId");
var clientSecret = pm.environment.get("apiClientSecret");
var unhashed = `${clientId.toLowerCase()}.${epochTimestamp.toLowerCase()}.${clientSecret.toLowerCase()}`;
console.log("Unhashed: " + unhashed);
var bytes = strToUtf8Bytes(unhashed);
console.log("Bytes: " + bytes);
var bytesHash = CryptoJS.SHA512(bytes);
console.log("Bytes Hash: " + bytesHash);
var clientIdHash = bytesHash.toString(CryptoJS.enc.Hex);
console.log("Client ID Hash: " + clientIdHash);
pm.collectionVariables.set("clientIdHash", clientIdHash);
function strToUtf8Bytes(str) {
const utf8 = [];
for (let ii = 0; ii < str.length; ii++) {
let charCode = str.charCodeAt(ii);
if (charCode < 0x80) utf8.push(charCode);
else if (charCode < 0x800) {
utf8.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f));
} else if (charCode < 0xd800 || charCode >= 0xe000) {
utf8.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f));
} else {
ii++;
// Surrogate pair:
// UTF-16 encodes 0x10000-0x10FFFF by subtracting 0x10000 and
// splitting the 20 bits of 0x0-0xFFFFF into two halves
charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(ii) & 0x3ff));
utf8.push(
0xf0 | (charCode >> 18),
0x80 | ((charCode >> 12) & 0x3f),
0x80 | ((charCode >> 6) & 0x3f),
0x80 | (charCode & 0x3f),
);
}
}
return utf8;
}
This executes as expected when I send a request. The problem is that my API requests are always rejected because the clientIdHash is wrong, which is why I put in the console.log() statements to try and debug it.
What I found is that whatever value I set for the unhashed variable, the value of the bytesHash variable is always the same.
For example, here is the console output for the above script with a "real" value for the unhashed variable (obviously not actually real as the clientId and clientSecret values are dummies):

And here's the console output if I change the unhashed value to "Literally anything":

As you can see, the console shows that the values of the unhashed and bytes values change, as you'd expect, but the values of the bytesHash and clientIdHash remain the same, even though they're derived from the bytes variable!
I've tried restarting Postman, updating it to the latest version (8.4), and deleting all of the files in c:\users\myusername\appdata\roaming\postman\cache, all to no avail. Across all of these attempts, whatever value I set the unhashed variable to, the bytesHash variable's value is always exactly as it appears in those screenshots.
I'm fully prepared for someone to point out to how I'm being monumentally stupid here, but at the moment I can't see why this is happening!