Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
caternuson opened this issue Dec 16, 2021 · 3 comments · Fixed by adafruit/Adafruit_CircuitPython_PyPortal#120

Comments

@caternuson
Copy link

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.

def _detect_content_type(self, headers):
if "content-type" in headers:
if "image/" in headers["content-type"]:
return CONTENT_IMAGE
for json_type in self._json_types:
if json_type in headers["content-type"]:
return CONTENT_JSON
return CONTENT_TEXT

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
Copy link
Collaborator

makermelissa commented Dec 16, 2021

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

@dearmash
Copy link

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants