Waboodoo / HTTP-Shortcuts

Android app to create home screen shortcuts that trigger arbitrary HTTP requests
https://http-shortcuts.rmy.ch
MIT License
1.17k stars 113 forks source link

[BUG] Parse JSON response & set variable, got incorrect value #206

Closed todd-xander closed 3 years ago

todd-xander commented 4 years ago

Steps To Reproduce

  1. Create a request,, it responses a JSON in shape like
    {
    "response": [{
        "id": 50196233593867390,
        ...
    }]
    }
  2. Add succeed process script
    const vehicles = JSON.parse(response.body);
    setVariable('tesla_vehicle_id', vehicles.response[0].id);
    showDialog('Vehicle ID updated '+ vehicles.response[0].id +'!','OK');
  3. Click to launch
  4. Dialog shows the correct id, but variable tesla_vehicle_id is wrong,

Expected behavior returned id is 50196233593867390

Actual behavior vadiable is 50196233593867392

Context (please complete the following information):

Waboodoo commented 3 years ago

Hello, I was not able to reproduce this issue. The only reason I can think of why this might be happening is because the variable 'tesla_vehicle_id' might not exist. Could it be that you have a typo in the name? The app currently does not produce an error message when trying to set a variable that doesn't exist.

todd-xander commented 3 years ago

Hi @Waboodoo , thks for reply

I am sure that the vadiable is created before running the shortcut, and type the variable name correctly in script. One more information, I've changed the actural number value for security. I could share it if helpful: 62383593501967390.

Waboodoo commented 3 years ago

Ok, I was able to reproduce the issue and did a bit of debugging. It seems the problem here is that the ID is larger than JavaScript's Number.MAX_SAFE_INTEGER, which leads to rounding issues when passing the number from the JavaScript code into the app's native code. I don't yet know how to fix this, or if it is even possible to fix this.

My suggestion for a workaround for now would be, if possible, to either use a shorter ID or to use a string ID instead of a number ID. You can also try to convert the number to a string before passing it into the setVariable function, like this:

const vehicles = JSON.parse(response.body);
const vehicleId = `${vehicles.response[0].id}`;
setVariable('tesla_vehicle_id', vehicleId);
showDialog('Vehicle ID updated '+ vehicleId +'!','OK');
Waboodoo commented 3 years ago

I have implemented a potential fix for this issue and included it in the 1.39.0 release. Hope it helps. I'm closing this issue for now, feel free comment and re-open if the problem still exists.