question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 <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:open
  • Created 4 years ago
  • Reactions:22
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
trplxcommented, Jul 26, 2022

@silkimen Any updates on this bug?

9reactions
alxpsrcommented, Jul 26, 2022

Need bugfix guys 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

POSTing valid JSON entities beside objects #324 - GitHub
The question: using the plugin, is there any way to send a valid JSON entity, but not an object. For example a string,...
Read more >
Sending a numeric value in http request with swift, adds ...
Swift Foundation's JSON serialization always formats numeric values to their full precision — so the double-precision number 1.6 gets ...
Read more >
Error JSONSerialization | Apple Developer Forums
Hi,. I have the following code: import UIKit. class ViewController: UIViewController {. //Our web service url.
Read more >
What is a JSON Injection and How to Prevent it? - Comparitech
We explain everything you need to know about JSON Injection attack, including how it works, and how to detect and prevent it.
Read more >
JSON Serialization - Unity - Manual
The JSON Serializer does not currently support working with unstructured JSON. That is, navigating and editing the JSON as an arbitrary tree of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found