邮递员四舍五入 BigInt [英] Postman rounding off BigInt

查看:12
本文介绍了邮递员四舍五入 BigInt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Postman 中设置一个较大的 int 值作为环境变量.

但 Postman 正在完善它.

有什么办法可以解决吗?

测试:

var response = JSON.parse(responseBody);for(var i=0; i<=response.Offerings.length ; i++) {if(response.Offerings[i].DisplayName == '三星 Galaxy S9+') {console.log(response.Offerings[i].Id);}}

API 响应:

响应:{奉献":[{金额":123,货币代码":CAD",显示名称":三星 Galaxy S9+",ID":1910256760591000500,语言":en-CA",业务线":[10048],MarketingViews":[],名称":三星 Galaxy S9+",报价类型":1,}]}

解决方案

问题

是的.所以它不是邮递员,它是基本的 Javascript.javascript 中的数字是双精度浮点数.基本上在 Javascript 中,最大的原始数字是 56 位,或者他们称之为 MAX_SAFE_INTEGER - 即 9,007,199,254,740,991.大多数语言都是 64 位 (9,223,372,036,854,775,807).postman 中发生的情况如下:

  • 您有一个需要超过 56 位的数字.
  • Javascript 希望帮助您并尽可能地转换它
  • JSON.parse(responseBody) 不能转换任何高于 56 位的数字.因此,它可以做到最好,并且您可以使用看起来像四舍五入的数字.

小例子是这样的:

让 x = 9223372036854775807让 y = BigInt(9223372036854775807)让文本 = x.toString();让 z = parseInt(文本, 10);console.log(z)//9223372036854776000

在 javascript 中有解决方案.即像

I am trying to set a large int value as an environment variable in Postman.

But Postman is rounding it off.

Is there any way I can handle this?

Tests:

var response = JSON.parse(responseBody); 
for(var i=0; i<=response.Offerings.length ; i++) {
    if(response.Offerings[i].DisplayName == 'Samsung Galaxy S9+') {
        console.log(response.Offerings[i].Id); 
    }
} 

API Response:

Response: { 
    "Offerings": [ 
        { 
            "Amount": 123,
            "CurrencyCode": "CAD",
            "DisplayName": "Samsung Galaxy S9+",
            "Id": 1910256760591000500,
            "Language": "en-CA",
            "LineOfBusinesses": [ 10048 ],
            "MarketingViews": [],
            "Name": "Samsung Galaxy S9+",
            "OfferType": 1,
        }
    ]
} 

解决方案

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)
    })();

这篇关于邮递员四舍五入 BigInt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆