0

Hello there I am beginner in python and I am getting this error when calling api on my local machine 'localhost'

from flask import Flask,request
from flask_restful import Resource,Api

app = Flask(__name__)
api = Api(app)

todos = {}
class HelloWorld(Resource):
    def get(self):
        return 'Hello, World War 3'

class Todo(Resource):
    def get(self, todo_id):
        return {todo_id: todos[todo_id]}

    def put(self,todo_id):
        todos[todo_id] : request.form['data']
        return {todo_id: todos[todo_id]}

api.add_resource(HelloWorld,'/')
api.add_resource(Todo,'/<string:todoId>')

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

and Here is the error I am getting while calling this api

raise JSONDecodeError("Expecting
value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Its a pretty small code and yet I am unable to catch the real issue. I am doing as it is on the official site of flask: Flask Site

I have seen other posts on the same issue but they are of high level which I am unable to understand. Any help would be appreciated, thanks

1 Answers1

1

There are a few issues in your code which are as follows:

  • Instead of keeping Todo list empty, add something and test like: todos = {'1': "Say hello"}

  • The API endpoint for todo is having todoId as a string object but in the get and put method you have todo_id as a parameter. Both should be same.

  • Instead of returning a single string it's better to return a JSON object. Like in your code replace 'Hello, World War 3' by something like {'msg': 'Hello, World War 3'}

NOTE: The last one is just for your information to keep all returns standard, and not an issue really.

Use following code for testing, and compare with your code in question. You will get an idea.

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

todos = {'1': "Say hello"}

class HelloWorld(Resource):
    def get(self):
        return {'msg': 'Hello, World War 3'}

class Todo(Resource):
    def get(self, todo_id):
        return {todo_id: todos[todo_id]}

    def put(self,todo_id):
        todos[todo_id] : request.form['data']
        return {todo_id: todos[todo_id]}

api.add_resource(HelloWorld,'/')
api.add_resource(Todo,'/<string:todo_id>')

if __name__ == '__main__':
    app.run(debug=True)
Rahul Shelke
  • 2,052
  • 4
  • 28
  • 50
  • I do get the result when I call – faizan shafiq Aug 30 '18 at 11:28
  • @faizanshafiq so I solved your issue or still persists? Your comment does not indicate anything related to the solution I gave... – Rahul Shelke Aug 30 '18 at 12:11
  • I did as you said but the error persisted. It seems like the api issue to me – faizan shafiq Sep 03 '18 at 11:09
  • I am working on another project right now. I will get back soon – faizan shafiq Sep 03 '18 at 11:16
  • When I write this `curl http://localhost:5000/todo1 -d "data=Remember the milk" -X PUT` I get this error `Invoke-WebRequest : A positional parameter cannot be found that accepts argument 'data=Remember the milk'. At line:1 char:1 + curl http://localhost:5000/todo1 -d "data=Remember the milk" -X PUT + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: ( :) [Invoke-WebRequest], ParameterBindingExce ption + FullyQualifiedErrorId : PositionalParamete rNotFound,Microsoft.PowerShell.Commands.Invo keWebRequestCommand` – faizan shafiq Sep 04 '18 at 06:04
  • @faizanshafiq you are using Microsoft PowerShell to execute curl request, and curl is an alias for PowerShell - Invoke-WebRequest, hence the error... refer this for more information: https://stackoverflow.com/a/48495916/512100 – Rahul Shelke Sep 04 '18 at 12:39