cordova-plugin-advanced-http
cordova-plugin-advanced-http copied to clipboard
[Bug] [iOS] JSON serialization manipulates number values in POST request body
Describe 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
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}
Is there any chance to get help for the described issue in this repository or is there just no support with this plugin for number values in POST request via iOS? This makes the plugin unuseable for my use-case as I have to send numbers in post request.
@silkimen ping
Hi Benjamin, I think this problem is caused by the accuracy problem of floating point values, because it's converted to a native floating point value before being serialized and sent via HTTP. I'll fix this when addressing #324 as it's related to this issue.
@silkimen any update about the fix?
When will it be fixed?
@silkimen Any updates on this bug?
Need bugfix guys :(