Skip to content
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

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

Open
esticle opened this issue Apr 25, 2019 · 1 comment
Open

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

esticle opened this issue Apr 25, 2019 · 1 comment
Assignees
Labels

Comments

@esticle
Copy link

esticle commented Apr 25, 2019

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!

@jest
Copy link

jest commented Apr 21, 2020

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'))

@dlo dlo added the bug label Oct 30, 2020
@dlo dlo self-assigned this Oct 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants