cordova-plugin-advanced-http icon indicating copy to clipboard operation
cordova-plugin-advanced-http copied to clipboard

[Bug] [iOS] JSON serialization manipulates number values in POST request body

Open benjamin-luescher opened this issue 5 years ago • 7 comments

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 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}

benjamin-luescher avatar Feb 21 '20 07:02 benjamin-luescher

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.

benjamin-luescher avatar Mar 05 '20 09:03 benjamin-luescher

@silkimen ping

benjamin-luescher avatar May 27 '20 06:05 benjamin-luescher

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 avatar Jun 25 '20 06:06 silkimen

@silkimen any update about the fix?

adriansaenz avatar Aug 24 '20 13:08 adriansaenz

When will it be fixed?

flashart92 avatar Dec 22 '21 13:12 flashart92

@silkimen Any updates on this bug?

trplx avatar Jul 26 '22 13:07 trplx

Need bugfix guys :(

alxpsr avatar Jul 26 '22 13:07 alxpsr