Giving the error "the JSON object must be str, not" bytes ""

I followed the tutorial on how to use elasticsearch using python (link = https://tryolabs.com/blog/2015/02/17/python-elasticsearch-first-steps/#contacto ), I ran into this error.

import json
    r = requests.get('http://localhost:9200') 
    i = 1
    while r.status_code == 200:
        r = requests.get('http://swapi.co/api/people/'+ str(i))
        es.index(index='sw', doc_type='people', id=i, body=json.loads(r.content))
        i=i+1

    print(i)

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

+4
source share
1 answer

You are using Python 3, and the blog post is targeting Python 2. The Python 3 function json.loads()expects unicode decoded text, not the original bytestring response that returns response.content.

, json.loads(), requests, JSON , response.json():

es.index(index='sw', doc_type='people', id=i, body=r.json())
+8

Source: https://habr.com/ru/post/1674114/


All Articles