Search code examples
pythonjsonrestaiohttp

Python aiohttp response numbers are altered


I am currently facing a strange behavior on a client-server app.

The server is written with python and uses aiohttp and returns a json with a lot of id's (number type). Every id is altered. I printed the response in server before returning and it contains the correct id.

Example ids: 481439269479645194, 190601736623226882, 338222603829510164

However in the browser network panel I can see, that the number arrives altered. The last digits are different.

For the exampes above: 481439269479645200, 190601736623226880, 338222603829510140

I am not able to determine where and how those id's change.


Solution

  • Numbers in browsers are stored as 64 bit floating point. Biggest integer that can be represented accurately is 9007199254740991. Your numbers are way larger than than and are approximated. For example:

    console.log([481439269479645194, 190601736623226882, 338222603829510164])
    

    will print:

    [481439269479645200, 190601736623226880, 338222603829510140]
    

    You can use BigInt instead to manipulate those numbers but you have to store them in JavaScript source and in JSON as strings:

    console.log([BigInt("481439269479645194"), BigInt("190601736623226882"), BigInt("338222603829510164")])