大佬教程收集整理的这篇文章主要介绍了request 请求大数精度丢失问题,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
request-promise-native
请求微服务接口返回 ID 精度丢失
> 1713166949059674112 => 1713166949059674000@H_450_6@
request-promise-native
发起请求时,当options.json
不为 false
会使用 JSON.parse
解析 body@H_450_6@
if (self._json) {
try {
response.body = JSON.parse(response.body, self._jsonReviver)
} catch (E) {
debug('invalid JSON received', self.uri.href)
}
}
public long getId() {
return id + 1713166949059674112L;
}
* 修改 controller 层添加 post 请求
@PostMapping("/greeTing_create")
public GreeTing createGreeTing(@requestParam(value = "name", DefaultValue = "World") String Name) {
return new GreeTing(counter.incrementAndGet(), String.format(template, Name));
}
curl http://localhost:8080/greeTing
@H_450_6@
curl -X POST http://localhost:8080/greeTing_create
@H_450_6@
{"id":1713166949059674120,"content":"Hello, World!"}
JSONbig
将 id
转化成字符串const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
const getOptions = {
'method': 'GET',
json: false,
'url': 'http://localhost:8080/greeTing',
};
const getRes = await rp(getOptions);
console.log('get result: ', jsonBigInt.parse(getRes));
JSON.parse
@H_450_6@
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
const postOptions = {
'method': 'POST',
'url': 'http://localhost:8080/greeTing_create',
json: { name: 'CassTime' },
};
const postRes = await rp(postOptions);
console.log('post result: ', jsonBigInt.parse(postRes));
JSONbig.parse()
替换 JSON.parse()
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
async function jsonBigreplaceParse() {
const oldParse = JSON.parse;
JSON.parse = jsonBigInt.parse;
const postOptions = {
'method': 'POST',
'url': 'http://localhost:8080/greeTing_create',
json: { name: 'CassTime' },
};
const postRes = await rp(postOptions);
console.log('post result: ', postRes);
JSON.parse = oldParse;
}
以上是大佬教程为你收集整理的request 请求大数精度丢失问题全部内容,希望文章能够帮你解决request 请求大数精度丢失问题所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。