How can I fix this error? json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

https://youtu.be/0iB5IPoTDts?t=4690

I am currently trying to like a product with my microservice per request. Here is the code from his video: https://github.com/scalablescripts/python-microservices

This is my code:

from flask import Flask, jsonify
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import UniqueConstraint
from dataclasses import dataclass
import requests

\#from markupsafe import escape
\#from jinja2 import escape

app = Flask(__name__)
app.config\["SQLALCHEMY_DATABASE_URI"\] = 'mysql://root:root@db/main'
CORS(app)

db = SQLAlchemy(app)

@dataclass
class Product(db.Model):
id: int
title: str
image: str

    id = db.Column(db.Integer, primary_key=True, autoincrement=False)
    title = db.Column(db.String(200))
    image = db.Column(db.String(200))

@dataclass
class ProductUser(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer)
product_id = db.Column(db.Integer)

    UniqueConstraint('user_id', 'product_id', name='user_product_unique')

@app.route('/api/products')
def index():
return jsonify(Product.query.all())

@app.route('/api/products/\<int:id\>/like', methods=\['POST'\])
def like(id):
req = requests.get('http://host.docker.internal:8000/api/user')
return jsonify(req.json())

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')

The API user is called which creates a random user, see code:

from django.contrib import admin
from django.urls import path

from .views import ProductViewSet, UserAPIView

urlpatterns = [
    path('products', ProductViewSet.as_view({
        'get': 'list',
        'post': 'create'
    })),
    path('products/<str:pk>', ProductViewSet.as_view({
        'get': 'retrieve',
        'put': 'update',
        'delete': 'destroy'
    })),
    path('user', UserAPIView.as_view())
]

and this is the class that will be executed:

class UserAPIView(APIView):
    def get(self, _):
        users = User.objects.all()
        user = random.choice(users)
        return Response({
            'id': user.id
        })

I get the following error in my terminal:

main-backend-1  | Traceback (most recent call last):
main-backend-1  |   File "/usr/local/lib/python3.11/json/decoder.py", line 353, in raw_decode
main-backend-1  |     obj, end = self.scan_once(s, idx)
main-backend-1  | StopIteration: 0
main-backend-1  | 
main-backend-1  | During handling of the above exception, another exception occurred:
main-backend-1  | 
main-backend-1  | Traceback (most recent call last):
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2464, in __call__
main-backend-1  |     return self.wsgi_app(environ, start_response)
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2450, in wsgi_app
main-backend-1  |     response = self.handle_exception(e)
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask_cors/extension.py", line 165, in wrapped_function
main-backend-1  |     return cors_after_request(app.make_response(f(*args, **kwargs)))
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1867, in handle_exception
main-backend-1  |     reraise(exc_type, exc_value, tb)
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/_compat.py", line 39, in reraise
main-backend-1  |     raise value
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2447, in wsgi_app
main-backend-1  |     response = self.full_dispatch_request()
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1952, in full_dispatch_request
main-backend-1  |     rv = self.handle_user_exception(e)
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask_cors/extension.py", line 165, in wrapped_function
main-backend-1  |     return cors_after_request(app.make_response(f(*args, **kwargs)))
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1821, in handle_user_exception
main-backend-1  |     reraise(exc_type, exc_value, tb)
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/_compat.py", line 39, in reraise
main-backend-1  |     raise value
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1950, in full_dispatch_request
main-backend-1  |     rv = self.dispatch_request()
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1936, in dispatch_request
main-backend-1  |     return self.view_functions[rule.endpoint](**req.view_args)
main-backend-1  |   File "/app/main.py", line 49, in like
main-backend-1  |     return jsonify(req.json())
main-backend-1  |   File "/usr/local/lib/python3.11/site-packages/requests/models.py", line 900, in json
main-backend-1  |     return complexjson.loads(self.text, **kwargs)
main-backend-1  |   File "/usr/local/lib/python3.11/json/__init__.py", line 346, in loads
main-backend-1  |     return _default_decoder.decode(s)
main-backend-1  |   File "/usr/local/lib/python3.11/json/decoder.py", line 337, in decode
main-backend-1  |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
main-backend-1  |   File "/usr/local/lib/python3.11/json/decoder.py", line 355, in raw_decode
main-backend-1  |     raise JSONDecodeError("Expecting value", s, err.value) from None
main-backend-1  | json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When I test the API in Postman, I should get an ID back (see video).

https://youtu.be/0iB5IPoTDts?t=4682

Back to Top