KeyError: content-length

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
SPIM
 
Posts: 3
Joined: Wed Apr 03, 2019 3:08 pm

KeyError: content-length

Post by SPIM »

I'm trying the PyPortal NASA Image Viewer and can't figure out why I am getting the error

File "adafruit_pyportal.py", line 586, in wget
KeyError: content-length

I've checked the NASA APOD key and it works fine (I can see the information display correctly in my browser). I've also checked all the information in secrets.py (other examples, like the weather or the quotes examples work fine).

Following the description of the example, I never saw the mentioned warning text when I changed unsafe_boot.py to boot.py. I also have to redo the renaming every time I change something in code.py because otherwise I will get the "No writable filesystemfound" error.

Thank you in advance for any idea how to troubleshoot this.

User avatar
Hethacker
 
Posts: 86
Joined: Fri Apr 05, 2019 9:02 pm

Re: KeyError: content-length

Post by Hethacker »

I have had this issue as well, and I have been trying to figure it out, I copied the intrusciotns and code exactly and that did not work, this is what I have found so far:

File "code.py", line 32, in <module>
File "code.py", line 29, in <module>
File "/lib/adafruit_pyportal.py", line 740, in fetch
File "/lib/adafruit_pyportal.py", line 737, in fetch
File "/lib/adafruit_pyportal.py", line 733, in fetch
File "/lib/adafruit_pyportal.py", line 733, in fetch
OSError:

so line 32 says that there is an error;
29 brings the code to Fetch
740 is gc. collect
737 is self.setbackground
733 is the actual text that says that there is no SD card inserted

User avatar
adafruit_support_carter
 
Posts: 29473
Joined: Tue Nov 29, 2016 2:45 pm

Re: KeyError: content-length

Post by adafruit_support_carter »

@Hethacker Please start a new thread for your issue.

User avatar
adafruit_support_carter
 
Posts: 29473
Joined: Tue Nov 29, 2016 2:45 pm

Re: KeyError: content-length

Post by adafruit_support_carter »

File "adafruit_pyportal.py", line 586, in wget
KeyError: content-length
The message is a little misleading. It's not complaining about API keys. Key here refers to the key in the key:value pair of the python dictionary which holds the response information from the request. It's basically saying there is no key for "content-length", which the code is trying to use to get that info here:
https://github.com/adafruit/Adafruit_Ci ... al.py#L586

This means the response isn't what was expected. We can see what is is by turning on debug. To do so, go into your nasa.py code and add this line before the while True: line

Code: Select all

pyportal._debug = True
If your issue repeats, you should hit the same error message. However, we will have some additional output we can look at to see what the actual response contains.

User avatar
SPIM
 
Posts: 3
Joined: Wed Apr 03, 2019 3:08 pm

Re: KeyError: content-length

Post by SPIM »

Thank you for your help. I had put it aside for a while after I had spent a couple of days trying different things out. I reconnected it yesterday after reading your response. The boot went better (I got the "risky" message). Things seemed to go fine, but no picture displayed. I did have the caption though, which was way better than before. I checked the source and saw that the daily image was not an image but a video, so I assumed that was why I didn't display. Today, it all works perfectly and I have the pretty picture of the Dolphin Cloud on Jupiter displaying.

User avatar
Hethacker
 
Posts: 86
Joined: Fri Apr 05, 2019 9:02 pm

Re: KeyError: content-length

Post by Hethacker »

I added the debug to the main code and got this response:

Set background to /nasa_background.bmp
No SD card found: [Errno 19] Unsupported operation
Free mem: 169216
Retrieving data...Reply is OK!
{"date":"2019-05-24","explanation":"An abundance of boulders litters the surface asteroid 101955 Bennu in this dramatic close-up from the OSIRIS-REx spacecraft. Taken on March 28 from a distance of just 3.4 kilometers (2.1 miles) the field of view is about 50 meters across while the light colored boulder at top right is 4.8 meters tall. Likely a loose conglomerate rubble pile asteroid, Bennu itself spans less than 500 meters. That's about the height of the Empire State Building. Mapping the near Earth asteroid since the spacecraft's arrival in December of 2018, the OSIRIS-REx mission plans a TAG (Touch-and-Go) maneuver for July 2020 to sample Bennu's rugged surface, returning the sample to planet Earth in September 2023. Citizen scientists have been invited to help choose the sample collection site.","hdurl":"https://apod.nasa.gov/apod/image/1905/p ... :"Boulders on Bennu","url":"https://apod.nasa.gov/apod/image/1905/p ... o-limb.jpg"}

original URL: https://apod.nasa.gov/apod/image/1905/p ... o-limb.jpg
convert URL: https://io.adafruit.com/api/v2/Hethacke ... o-limb.jpg
Fetching stream from https://io.adafruit.com/api/v2/Hethacke ... o-limb.jpg
{'access-control-max-age': '1728000', 'etag': 'W/"b601ce3f76fcd0d7497cbf377c1a36e6"', 'access-control-expose-headers': 'X-Pagination-Limit, X-Pagination-Start, X-Pagination-End, X-Pagination-Count, X-Pagination-Total', 'referrer-policy': 'strict-origin-when-cross-origin', 'content-transfer-encoding': 'binary', 'x-aio-proxy': '1', 'content-length': '156882', 'strict-transport-security': 'max-age=31536000', 'content-type': 'image/bmp', 'x-download-options': 'noopen', 'access-control-request-method': '*', 'connection': 'close', 'access-control-allow-methods': 'POST, PUT, DELETE, GET, OPTIONS, PATCH', 'access-control-allow-headers': 'DNT, Origin, X-Requested-With, X-AIO-Key, Content-Type, Accept, Authorization', 'content-disposition': 'attachment', 'cache-control': 'private', 'date': 'Sat, 25 May 2019 01:06:40 GMT', 'server': 'nginx', 'x-content-type-options': 'nosniff', 'x-BANNED-protection': '1; mode=block', 'access-control-allow-origin': '*', 'x-frame-options': 'SAMEORIGIN', 'x-request-id': 'a62825c2-57cb-4667-a54c-790f54ef0ca8', 'x-aio-worker': 'io-rails-1', 'x-runtime': '0.403165', 'x-permitted-cross-domain-policies': 'none'}
Saving data to /sd/cache.bmp
[Errno 30] Read-only filesystem
Traceback (most recent call last):
File "code.py", line 34, in <module>
File "code.py", line 31, in <module>
File "adafruit_pyportal.py", line 740, in fetch
File "adafruit_pyportal.py", line 737, in fetch
File "adafruit_pyportal.py", line 733, in fetch
File "adafruit_pyportal.py", line 733, in fetch
OSError:

No writable filesystem found for saving datastream. Insert an SD card or set internal filesystem to be unsafe by setting 'disable_concurrent_write_protection' in the mount options in boot.py

User avatar
kevinjwalters
 
Posts: 1026
Joined: Sun Oct 01, 2017 3:15 pm

Re: KeyError: content-length

Post by kevinjwalters »

adafruit_support_carter wrote:
File "adafruit_pyportal.py", line 586, in wget
KeyError: content-length
The message is a little misleading. It's not complaining about API keys. Key here refers to the key in the key:value pair of the python dictionary which holds the response information from the request.
FYI, I put in Improve error handling for HTTP response errors and exception handling for JSON parse parsing in fetch() #37 to eventually improve the handling of HTTP response header parsing.

User avatar
kevinjwalters
 
Posts: 1026
Joined: Sun Oct 01, 2017 3:15 pm

Re: KeyError: content-length

Post by kevinjwalters »

@SPIM It sounds like you're aware of the requirement for one one of the following:
  • Rename unsafe_boot.py to boot.py,
  • xor add an SD card.
The first one requires a lot of discipline for not changing CIRCUITPY contents while the code is running. The second one will be more robust because of that.

Your debug output suggests you have an SD card present? The /sd directory is only prepended if SD card is detected (adafruit_pyportal.py line 753)

Code: Select all

Saving data to /sd/cache.bmp
[Errno 30] Read-only filesystem
Is that the case? Might be worth checking you can write to it from a different machine?

User avatar
SPIM
 
Posts: 3
Joined: Wed Apr 03, 2019 3:08 pm

Re: KeyError: content-length

Post by SPIM »

Just to follow up: I still get the "KeyError: content-length" on some days. So I am thinking (I have not tested this theory), that it might be linked to the format of the picture of the day (sometimes it's a video that won't display) or something in the url.

The Rename unsafe_boot.py to boot.py is no longer an issue.

User avatar
kevinjwalters
 
Posts: 1026
Joined: Sun Oct 01, 2017 3:15 pm

Re: KeyError: content-length

Post by kevinjwalters »

I'm getting the KeyError too. Looking more closely at your and my output from the serial console, this is a problem / bug with the adafruit.io service that converts the image not with fetching it from NASA. Here's my error but my line numbers will be different because I'm running a customized adafruit_pyportal:

Code: Select all

soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Set background to  /pyportal_startup.bmp
ESP firmware: bytearray(b'1.2.2\x00')
Connecting to AP SSID-HERE
Set background to  /nasa_background.bmp
Retrieving data...Reply is OK!
original URL: https://apod.nasa.gov/apod/image/1905/M95_HstEsoGendler_960.jpg
convert URL: https://io.adafruit.com/api/v2/USER/integrations/image-formatter?x-aio-key=eaccbba70eb148c0abf7878ad8a14bf6&width=320&height=240&output=BMP16&url=https://apod.nasa.gov/apod/image/1905/M95_HstEsoGendler_960.jpg
Fetching stream from https://io.adafruit.com/api/v2/USER/integrations/image-formatter?x-aio-key=eaccbba70eb148c0abf7878ad8a14bf6&width=320&height=240&output=BMP16&url=https://apod.nasa.gov/apod/image/1905/M95_HstEsoGendler_960.jpg
Traceback (most recent call last):
  File "code.py", line 59, in <module>
  File "code.py", line 56, in <module>
  File "adafruit_pyportal.py", line 791, in fetch
  File "adafruit_pyportal.py", line 788, in fetch
  File "adafruit_pyportal.py", line 784, in fetch
  File "adafruit_pyportal.py", line 778, in fetch
  File "adafruit_pyportal.py", line 617, in wget
KeyError: content-length



Press any key to enter the REPL. Use CTRL-D to reload.

I've added notes on this to Improve error handling for HTTP response errors and exception handling for JSON parse parsing in fetch() #37.

User avatar
kevinjwalters
 
Posts: 1026
Joined: Sun Oct 01, 2017 3:15 pm

Re: KeyError: content-length

Post by kevinjwalters »

I've made my PyPortal running NASA code a gift now so no longer have it. If you get KeyError: content-length again check https://status.adafruit.com/ ASAP to see if it's indicating a problem with any of the IO services which need a click to expand them.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”