Can't explain but I kept getting the AttributeError. The middle conditional statement would not catch it.
Maybe because JSON is a complex object ?
The length 16 might be the usual header and an empty body. I didn't check the text. Only the length.