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

7
  • 1
    Please show the full traceback. I'm not sure how that error relates to your question but it doesn't look like a Flask issue Commented Aug 30, 2018 at 10:30
  • What do you think happens the first time you do http://localhost:5000/1? Commented Aug 30, 2018 at 10:32
  • your HelloWorld resource is not returning a JSON object. You are instead returning a str type. It should instead be return {'hello': 'Hello, World War 3'} Commented Aug 30, 2018 at 10:36
  • I did as you said in curl but it returns error 500 Commented Aug 30, 2018 at 10:38
  • @Tushortz I corrected it but the issue is still there Commented Aug 30, 2018 at 10:41

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

6 Comments

I do get the result when I call
@faizanshafiq so I solved your issue or still persists? Your comment does not indicate anything related to the solution I gave...
I did as you said but the error persisted. It seems like the api issue to me
I am working on another project right now. I will get back soon
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.