Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days.
0

PyPortal json parsing example broken
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

PyPortal json parsing example broken

by dearmash on Thu Jan 13, 2022 2:25 pm

I was setting up the example in https://learn.adafruit.com/electronic-h ... h-pyportal

As-is this guide will not work.

A few issues I uncovered

https://raw.githubusercontent.com/adafr ... 01_13.json is the URL (or similar) that it tries to connect to.

Code: Select all | TOGGLE FULL SIZE
< HTTP/2 200
< cache-control: max-age=300
< content-security-policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
< content-type: text/plain; charset=utf-8


Unfortunately this response is returning text/plain instead of an expected format in adafruit_portalbase

The quickest and easiest fix would be for the adafruit github to return an expected json content-type header.

Assuming that is not possible, I have some simple code I could wrap into a pull request for portalbase network.py
Code: Select all | TOGGLE FULL SIZE
            if "text/plain" in headers["content-type"] and json_path:
                return CONTENT_JSON


With a little bit of plumbing, if a request is made with json path, but the response is plain, treat it as json anyway. Not sure if that's desired. The PyPortal library does its own fetch logic, separate from PortalBase, and would need to be updated too.

The main reason why I'm putting this in a forum post, vs a github issue is it's either fixed via some github json setting, or in code it touches multiple projects and should be committed at the same time.

A side note, an unintended breakage found in bitmap label parsing came up when trying to parse json as text. in displaytext bitmaplabel If the text is empty / just new lines, then left is always set to None for this block of code and will crash at the bottom when trying to subtract.

Elsewhere there is a check if left is None: left = 0. I'm conflicted whether left should be set to zero, or to x_start, which seems more appropriate.

dearmash
 
Posts: 49
Joined: Mon Nov 28, 2011 4:20 pm

Re: PyPortal json parsing example broken

by adafruit_support_carter on Thu Jan 13, 2022 2:44 pm

Please go ahead and open an issue on Github. This has happened before and seems to be due to changes made in the various libraries. Older versions were less fancy and guides could assume they got json back. Now it's fancier and is trying to detect json vs. plain text. But that logic is breaking older guides.

Actually not sure best repo to open issue in, but PortalBase seems like the best place to start at least:
https://github.com/adafruit/Adafruit_Ci ... ase/issues

There is a similar issue here:
https://github.com/adafruit/Adafruit_Ci ... /issues/57

adafruit_support_carter
 
Posts: 22293
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.