[Bug] [iOS] JSON serialization manipulates number values in POST request body
See original GitHub issueDescribe the bug
There’s a problem of serializing a POST body with number values (iOS only). By sending a number value, for example: 0.1
in the POST body and by using JSON as dataSerializer in this plugin, I receive the passed 0.1
as 0.10000000000000001
on the server side erroneously. The problem only occurs by setting the dataSerializer to json.
System info
- affected HTTP plugin version: 2.4.1
- affected platform(s) and version(s): iOS 12.4.3
- affected device(s): iPhone 6
- cordova version: 9.0.0
- cordova platform version(s): cordova-ios 5.1.1
Minimum viable code to reproduce For reproduction you can make a simple POST request to a server. I created a small node server to log the request post body to console:
const http = require("http");
const server = http.createServer((req, res) => {
let body = '';
req.on('data', chunk => {
body += chunk.toString(); // convert Buffer to string
});
req.on('end', () => {
console.log(body);
res.end('ok');
});
});
server.listen(8888);
Now send a POST request with a number value causing the double-precision problem like 0.1
in the body and set the dataSerializer to json. (Replace <your-ip-address> with your ip address and make sure your iPhone is in the same network as your node-server / desktop pc)
cordova.plugin.http.setDataSerializer('json');
cordova.plugin.http.post('http://<your-ip-address>:8888', { myNumber: 0.1 }, {}).then(console.log, console.log);
In your server console you get:
{"myNumber":0.10000000000000001}
but the expected request would be:
{"myNumber":0.1}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:22
- Comments:7 (1 by maintainers)
@silkimen Any updates on this bug?
Need bugfix guys 😦