adafruit / Adafruit_CircuitPython_PortalBase

Base Library for the Portal-style libraries.
MIT License
18 stars 17 forks source link

Need special case for SVG files in network.check_reponse()? #57

Closed caternuson closed 2 years ago

caternuson commented 2 years ago

This guide: https://learn.adafruit.com/pyportal-discord-online-count/overview uses code: https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/PyPortal_Discord/code.py that parses the XML found in a downloaded SVG using regex. That requires a return type of CONTENT_TEXT from check_response() for the PyPortal library to work as expected: https://github.com/adafruit/Adafruit_CircuitPython_PyPortal/blob/8031c95bbb209a2e4ad5bec7628fd02a4614da40/adafruit_pyportal/__init__.py#L310

However, this library will end up returning CONTENT_IMAGE since the fetch headers will contain content-type: image/svg_html. https://github.com/adafruit/Adafruit_CircuitPython_PortalBase/blob/6a94293fa97b3a0daa99bf715c8e83c1179cd0d5/adafruit_portalbase/network.py#L493-L500

That breaks the code for the above guide.

Opening issue here, but could maybe be fixed in one of the higher level libraries, like PyPortal?

makermelissa commented 2 years ago

I'd suggest adding an optional parameter to this library to force a particular content type which would make it skip the autodetecting.

dearmash commented 2 years ago

I second the suggestion of forcing a content type. I have a similar issue noted in https://forums.adafruit.com/viewtopic.php?f=59&t=187225 where a json response from a server is identified as text/plain and can't be correctly parsed.

My solution was to force json if the type was text/plain if the json_path is specified for parsing the response. It makes just as much sense, and can be solved the same way, by specifying a content_type_override of some sort.

FoamyGuy commented 2 years ago

I am working on a fix for this, will PR soon.