Search code examples
pythonjsonapiflask-restfulflask-restplus

error value: Expecting value: line 1 column 1 (char 0)


I am getting error value:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0).

I have a app Flask. I want to use Rest api. I have a app.py file and api.py file.I am writing a get method in api.pyfile. This method have a input parameter(P_ID) and output of this method is JSON type. This method run correctly. Now, I want to use requests.get in app.py but I encountered this error.

my code in app.py file is:

@app.route('/edit/<int:P_ID>', methods=['GET','POST'])
def update(P_ID):
    if request.method=='GET':
        info = requests.get('http://localhost:5000/edit/<int:P_ID>')
        info = info.json()
    return info

my api.py file is:

class ProductEdit(Resource):
    def get(self,P_ID):
        cursor=conn.cursor()
        cursor.execute("SELECT * FROM Tbl_product WHERE P_ID=?",(P_ID))
        columns = [column[0] for column in cursor.description]
        results = []
        for row in cursor.fetchall():
            results.append(dict(zip(columns, row)))
        rest = jsonify(results)
        return rest

api.add_resource(ProductEdit , '/edit/<int:P_ID>')

if __name__ == '__main__':
    flask_app.run(debug=True)

Can you help me?


Solution

  • info = requests.get('http://localhost:5000/edit/<int:P_ID>')
    

    Should be (for Python3.6 or greater)

    info = requests.get(f'http://localhost:5000/edit/{P_ID}')
    

    The <int:P_ID> syntax is for flask routing/parsing - it's not a normal python string formatting syntax

    You should also check the response from the request, a simple way to do it is to check the status code

    if info.status_code != 200:
        # return a helpful error message here