Home > Enterprise >  Python/Django requests JSONDecodeError: Expecting value: line 1 column 1 (char 0)
Python/Django requests JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Time:10-19

I am trying make Microservice warehouse / store. Everything is ok, but when I want to connect celery to my project, I am getting error Expecting value: line 1 column 1 (char 0) when trying to start celery

@shared_task
def shop_sync():
    url = 'http://warehouse:8001/authors/'
    response_author = requests.get(url=url).json()
    while 1:
        for counter, data in enumerate(response_author['results']):
            Author.objects.get_or_create(
                id=data['id'],
                defaults={
                    'id': data['id'],
                    'first_name': data['first_name'],
                    'last_name': data['last_name']
                }
            )

        if response_author['next']:
            response_author = requests.get(response_author['next']).json()
        else:
            break

    url = 'http://warehouse:8001/genres/'
    response_genre = requests.get(url).json()
    while 1:
        for counter, data in enumerate(response_genre['results']):
            Genre.objects.get_or_create(
                id=data['id'],
                defaults={
                    'id': data['id'],
                    'name': data['name']
                }
            )

        if response_genre['next']:
            response_genre = requests.get(
                response_genre['next']
            ).json()
        else:
            break
    url = 'http://warehouse:8001/books/'
    response = requests.get(url).json()
    while 1:
        for counter, data in enumerate(response['results']):
            book, created = Book.objects.get_or_create(
                id=data['id'],
                defaults={
                    'id': data['id'],
                    "title": data['title'],
                    "description": data['description'],
                    "image": data['image'],
                    "language": data['language'],
                    "status": data['status'],
                    "price": data['price'],
                    "isbn": data['isbn'],
                    "pages": data['pages'],
                    "created": data['created'],
                    "available": data['available'],
                    "quantity": data['quantity'],
                    "genre": Genre.objects.get(id=data['genre'])
                }
            )

            if not created:
                book.title = data['title']
                book.description = data['description']
                book.image = data['image']
                book.language = data['language']
                book.status = data['status']
                book.price = data['price']
                book.isbn = data['isbn']
                book.pages = data['pages']
                book.created = data['created']
                book.available = data['available']
                book.quantity = data['quantity']
                book.genre = Genre.objects.get(id=data['genre'])
                book.save()

            for i in data['author']:
                author = Author.objects.get(id=i)
                book.author.add(author)

        if response['next']:
            response = requests.get(response['next']).json()
        else:
            break
    print('Sync is done')

Exception:

Traceback (most recent call last):
    File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 450, in trace_task
    R = retval = fun(*args, **kwargs)
    File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 731, in __protected_call__
    return self.run(*args, **kwargs)
    File "/code/shop/tasks.py", line 105, in shop_sync
    response = requests.get(url=url).json()
    File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 910, in json
    return complexjson.loads(self.text, **kwargs)
    File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
    File "/usr/local/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "/usr/local/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

CodePudding user response:

To avoid such issues in your code you should first check the response status code and then based on the status do appropriate action. Such as :-

url = 'http://warehouse:8001/books/'
response = requests.get(url)
if response.status_code != 200:
   return

response_data = response.json()
while 1:
    for counter, data in enumerate(response_data['results']):
        book, created = Book.objects.get_or_create(
            id=data['id'],
            ...
            ...
        )
  • Related