Http endpoint error on decoding json

Hi
What I’m trying to do:
I have a small datalogger that posts me sensor measurements formatted in JSON to my http endpoint.
All work fine until few days.
Now I have the following error

Incoming API request from Frankfurt am Main, Germany.
ValueError: invalid literal for int() with base 10: '-646372944295432567596440000000000' at /lib/lib-python/3/json/scanner.py, line 54called from /lib/lib-python/3/json/decoder.py, line 185called from /lib/lib-python/3/json/scanner.py, line 38called from /lib/lib-python/3/json/decoder.py, line 229called from /lib/lib-python/3/json/scanner.py, line 40called from /lib/lib-python/3/json/decoder.py, line 185called from /lib/lib-python/3/json/scanner.py, line 38called from /lib/lib-python/3/json/scanner.py, line 67called from /lib/lib-python/3/json/decoder.py, line 355called from /lib/lib-python/3/json/decoder.py, line 339called from /lib/lib-python/3/json/__init__.py, line 361called from /libanvil/anvil/_server.py, line 1280

What I’ve tried and what’s not working:
To understand why the json can not be decoded, I have tried to print the raw http request but I cannot do this. The error occur before I can print the resquest body. I think the json decoder is called before the call to the user-defined method.
Even this result in an error:

@anvil.server.http_endpoint('/prisma',methods=["POST"])
def get_datas():
  print("hello")

Have you any suggest ?

Thanks

When everything goes well the automatic conversion to json is fine, but sometimes everything doesn’t go well.

That’s why I don’t use it. I get the payload and convert it. You can see here an example: Http_endpoint [POST] Issues Publishing to Data Table - #4 by stefano.menci

1 Like

How can I prevent the JSON from being parsed automatically? I think it gets parsed (and throws an error) before it runs my first line of code. So I can’t do anything.

Unfortunately, the datalogger is not open source, I cannot make modifications to the header of the POST request.

I cannot use “anvil.server.request.body.getbytes ()” because the error is thrown before running the first line of my code.

Hi @m.daviau,

It looks like this may be an issue with the JSON parser in the Basic Python 3 environment. Does this occur in the Full Python environment?

In general, however, Anvil should be able to cope with application/json payloads that the parser barfs on. I’ve made a change, which will go live in the next few days, which won’t attempt to do built-in JSON parsing unless and until you access the body_json attribute.

2 Likes

I have not try it with full python because I am in freeplan.
I will upgrade for the test and I will keep you inform.

Great idea to parse json only when body_json is called. This allows us to add error handling in our own code.

Thanks !!

1 Like

Update :
There is no error in the full python environment

It seems that the JSON parser in the basic Python 3 cannot convert
‘-646372944295432567596440000000000’ to integer.

I don’t understand why my datalogger sent me this huge number as a record but this is not related to Anvil ! :laughing:

Thanks you @meredydd and @stefano.menci