“This is the 17th day of my participation in the First Challenge 2022.
background
- NPM package used by BFF Client
request-promise-native
Request microservice interface return ID precision missing
1713166949059674112 = > 1713166949059674000
Why is it missing?
- Math.pow(2, 53) is the largest integer that can be accurately represented in JS. Math.pow(2, 53) If 9007199254740992 is greater than 9007199254740992, the precision may be lost
- Reference: zhuanlan.zhihu.com/p/100353781
request-promise-native
Initiate a request whenoptions.json
Don’t forfalse
Will useJSON.parse
Parsing the body
if (self._json) {
try {
response.body = JSON.parse(response.body, self._jsonReviver)
} catch (e) {
debug('invalid JSON received', self.uri.href)
}
}
Copy the code
Minimum demo
Build service API
Build Java Web Api
- Reference: Building a RESTful Web Service
- Modify the Service layer to make the id minimum greater than the JS precision limit
public long getId(a) {
return id + 1713166949059674112L; } * Modify the Controller layer to add post requests@PostMapping("/greeting_create")
public Greeting createGreeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
Copy the code
Second, the request
- GET request:
curl http://localhost:8080/greeting
- POST request:
curl -X POST http://localhost:8080/greeting_create
{"id":1713166949059674120."content":"Hello, World!"}
Copy the code
The solution
1. Obtain the string of the response bodyJSONbig
将 id
Convert to a string
- Advantages: Only the current request is affected
- Disadvantages: Does not support POST request mode,
- Sending parameters through JSON is not supported
- Passing parameters through form + JSON: false requires backend interface support
- A GET request
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));
Copy the code
- POST request: not supported, json is occupied, must be executed
JSON.parse
const rp = require('request-promise-native');
const jsonBigInt = require('json-bigint');
const postOptions = {
'method': 'POST'.'url': 'http://localhost:8080/greeting_create'.json: { name: 'test'}};const postRes = await rp(postOptions);
console.log('post result: ', jsonBigInt.parse(postRes));
Copy the code
2. UseJSONbig.parse()
replaceJSON.parse()
- Advantages: Simple implementation, support POST
- Disadvantages: Affects all json.parse () parsing
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: 'test'}};const postRes = await rp(postOptions);
console.log('post result: ', postRes);
JSON.parse = oldParse;
}
Copy the code
~
Thanks for reading!
~
Learn interesting knowledge, meet interesting friends, shape interesting soul!
Hello everyone, I am the author of “programming Samadhi”, I am king Yi, my public account is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!