pinboard.py icon indicating copy to clipboard operation
pinboard.py copied to clipboard

Python3: TypeError: the JSON object must be str, not 'bytes'

Open esticle opened this issue 5 years ago • 1 comments

Anybody run into this one? Must be environmental but dependencies appear to be up to date. I am running 2.1.8:

$ python3
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> api_token = 'XXXX:YYYYYYYYYYYYYYYYYYYY'
>>> import pinboard
>>> pb = pinboard.Pinboard(api_token)
>>> print (pb)
<pinboard.pinboard.Pinboard object at 0x7f8a2330d198>
>>> pb.posts.update()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/pinboard/pinboard.py", line 196, in __call__
    json_response = json.load(response)
  File "/usr/lib/python3.5/json/__init__.py", line 268, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
>>>

Thanks!

esticle avatar Apr 25 '19 10:04 esticle

json.load() requires str, but HTTPResponse.read() returns bytes. In Python 3.6 it should work, as json.load() has additional logic to decode the bytes. https://docs.python.org/3/whatsnew/3.6.html#json.

In older versions, a patch is needed. Replace json.load(response) in pinboard/pinboard.py and bin/pinboard with

json.loads(response.read().decode('utf-8'))

or use parse_response=False and parse in your code:

resp = pb.posts.update(parse_response=False)
json.loads(resp.read().decode('utf-8'))

jest avatar Apr 21 '20 13:04 jest