The problem
Yeah. So it isn't postman that is rounding off it is the base Javascript. Numbers in javascript are double precision floats. Bascially in Javascript the largest primitive number is 56 bits or as they call it MAX_SAFE_INTEGER - which is 9,007,199,254,740,991. Most languages are 64 bits(9,223,372,036,854,775,807).
What happens in postman is the following:
- You have a number that requires more than 56 bits.
- Javascript wants to help you and converts it as good as it can
- JSON.parse(responseBody) can't convert any number higher than 56 bits. So it does it best and you and up with something that looks like a rounded number.
Small Example would be this:
let x = 9223372036854775807
let y = BigInt(9223372036854775807)
let text = x.toString();
let z = parseInt(text, 10);
console.log(z) // 9223372036854776000
There are solutions to this in javascript. Namely a couple of libraries like "json-bigint". However including that in Postman is not exactly easy. Ironically Postman already makes use of BigInteger, but it does not work when the code you're executing is base javascript. But it is still the reason why @Plurimus solution should work.
What to do
Whenever you need a library in Postman there are ways to load it in using requests. Here users demonstrate how they load libraries mainly stored on cloudflare. And spoiler - json-bigint - is not there. It's an npmjs library which only makes it harder to add.
One issue you are definitely going to run into is that Postman pre-requests are async. Which means your library might get loaded, but code that was supposed to use the library has already been executed. Luckily someone already found a hack for that.
A solution
In my solution I have a link to a library at my own personal github that I want to use. Always fork stuff like this. I then use postman get the code. And uses eval to override the JSON object. There is some preliminary code that shows the value before and after loading the library.
The request is run in method called main which is an async method added to the pre-request in order to make sure that everything runs synchronously inside that function.
console.log('hello');
var libraryLink = 'https://gist.githubusercontent.com/kentkost/f2699eb1dd41e2eb6203f0d0c77a987e/raw/bffae74aa3827ea195c10cf83bce4d78017bdf2d/postman-bigint.js'
let dummyJson = '{ "value" : 9223372036854775807, "v2": 123 }'
let obj = JSON.parse(dummyJson)
console.log(obj.value);
console.log(JSON.stringify(obj));
const _dummy = setInterval(() => {}, 300000);
function sendRequest(req) {
return new Promise((resolve, reject) => {
pm.sendRequest(req, (err, res) => {
if (err) {
return reject(err);
}
return resolve(res);
})
});
}
(async function main() {
let result1 = await sendRequest(libraryLink);
eval(result1.text());
let newObj = JSON.parse(dummyJson);
console.log(newObj.value);
pm.environment.set("someVar", newObj.value);
console.log(JSON.stringify(newObj));
console.log('bye');
clearInterval(_dummy)
})();
