adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 36 forks source link

Adafruit_Hue-Circuitpython_Requests - File "adafruit_requests.py", line 419, in json #125

Closed cn-schuh closed 3 months ago

cn-schuh commented 1 year ago

Hi, i am not sure this is the correct way.

Currently i am experiencing with the adafruit_hue Circuitpython lib

I am using a Adafruit Feather RP2040 with a AirLift FeatherWing – ESP32 WiFi Co-Processor. (optianally i did a test with an EdgeBadge and and a Airlift Breakout). I tested Circuitpython 7.3.3 and 8.0.0 - Beta 6 and the controller should be compatible, i guess.

Following this guide: https://learn.adafruit.com/pyportal-phi ... controller or other available guides regarding Adafruit_Hue.

My code fails always with the same error:

Traceback (most recent call last): File "code.py", line 49, in File "/lib/adafruit_hue.py", line 109, in discover_bridge File "/lib/adafruit_requests.py", line 419, in json ValueError: syntax error in JSON

during executing of functions like ip = my_bridge.discover_bridge() or my_bridge.get_groups() and all other HUE functions.

The JSON file of my HUE Box is arround 30 KB. i guess it must be something related on the format. The JSON itself is valid.

To be sure the problem is not related on the Adafruit_HUE lib, I did another test, without Adafruit_HUE:

Usage of the test was the guide below: https://learn.adafruit.com/adafruit-air ... et-connect

Result: The first json cames from the guide above, the second and third JSON from my HUE bridge.

Fetching json from http://api.coindesk.com/v1/bpi/currentprice/USD.json

{'time': {'updated': 'Jan 11, 2023 08:28:00 UTC', 'updatedISO': '2023-01-11T08:28:00+00:00', 'updateduk': 'Jan 11, 2023 at 08:28 GMT'}, 'disclaimer': 'This data was produced from the CoinDesk BANNED Price Index (USD). Non-USD currency data converted using hourly conversion rate from openexchangerates.org', 'bpi': {'USD': {'code': 'USD', 'description': 'United States Dollar', 'rate_float': 17428.1, 'rate': '17,428.0921'}}}

Fetching json from http://192.168.178.26/api/-lxlSLEO1If8k ... ZdrJJNdU4/

Traceback (most recent call last): File "code.py", line 71, in File "adafruit_requests.py", line 419, in json ValueError: syntax error in JSON

Fetching json from http://192.168.178.26/api/-lxlSLEO1If8k ... 4/lights/7

Traceback (most recent call last): File "code.py", line 71, in File "adafruit_requests.py", line 419, in json ValueError: syntax error in JSON

Code done running.

As you can see, the error File "adafruit_requests.py", line 419, in json ValueError: syntax error in JSON is always identical.

anecdata commented 1 year ago

You may be experiencing #38. Can you print the headers that come back in the response to the request (print(r.headers))? It would also be helpful for test purposes to print the response content (or text) instead of the JSON (print(r.content)), you may see partial (or no) data.

cn-schuh commented 1 year ago

From Python: Fetching JSON data from http://192.168.178.26/api/-lxlSLEO1If8kEjry2ar2jrv6c4WhZEZdrJJNdU4 {'date': 'Wed, 11 Jan 2023 15:02:18 GMT', 'pragma': 'no-cache', 'expires': 'Mon, 1 Aug 2011 09:00:00 GMT', 'content-type': 'application/json', 'x-content-type-options': 'nosniff', 'server': 'nginx', 'connection': 'close', 'access-control-allow-methods': 'POST, GET, OPTIONS, PUT, DELETE, HEAD', 'access-control-allow-headers': 'Content-Type', 'content-security-policy': "default-src 'self'", 'access-control-allow-credentials': 'true', 'access-control-max-age': '3600', 'cache-control': 'no-store', 'access-control-allow-origin': '*', 'x-frame-options': 'SAMEORIGIN', 'x-xss-protection': '1; mode=block', 'referrer-policy': 'no-referrer'}

From Hue Bridge: Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE, HEAD Access-Control-Allow-Origin: * Access-Control-Max-Age: 3600 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, no-store Connection: close Content-Security-Policy: default-src 'self' Content-Type: application/json Date: Wed, 11 Jan 2023 14:56:16 GMT Expires: Mon, 1 Aug 2011 09:00:00 GMT Pragma: no-cache, no-cache Referrer-Policy: no-referrer Server: nginx X-Content-Type-Options: nosniff X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,/;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: de,en-US;q=0.7,en;q=0.3 Connection: keep-alive Host: 192.168.178.26 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:108.0) Gecko/20100101 Firefox/108.0

cn-schuh commented 1 year ago

One further info. I can fire a event once a time before the syntax error occurs. For example: my_bridge.set_light(48, on=True) is turning light 48 on, but than code crashed.

anecdata commented 1 year ago

Those seem to be OK, except that there's no content-length or chunked encoding. There is an open PR #123 to handle that case. You could test the library code in that PR.

What do you get when you print(r.content) instead of print(r.json)?

cn-schuh commented 1 year ago

response.json(): Traceback (most recent call last): File "code.py", line 54, in File "adafruit_requests.py", line 419, in json ValueError: syntax error in JSON

response.content: b''

I guess both contents are empty

anecdata commented 1 year ago

You've verified that the JSON is valid, so I'd suggest downloading the requests library code from the PR above and seeing if that helps.

(wait a full minute or more for the response, or put in a smaller timeout in requests, e.g., r = requests.get(TEXT_URL, timeout=10))

cn-schuh commented 1 year ago

Thanks, i will run some tests with PR https://github.com/adafruit/Adafruit_CircuitPython_Requests/pull/123 and reply with an update.

cn-schuh commented 1 year ago

Currently the test seems to be stable. The error can be reproduced only when request.get will be executed quite often or during or the json file is huge. But this can be handled. Definetely the Timeout is an improvement. Thanks.

anecdata commented 3 months ago

Closing as fixed by #123