I am trying to follow the documentation of flask-restful and tried to run the following code.
from flask import Flask, request
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
todos = {}
class TodoSimple(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(TodoSimple, '/<string:todo_id>')
if __name__ == '__main__':
app.run(debug=True)
But when I tried to run it with "http://127.0.0.1:5000/todo1" URL it responds with the message "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.". What am I doing wrong with the code. Please help.
The problem is in the way you defined the url route for your resource. Right now you are trying to access it via http://127.0.0.1:5000/todo1
but you have defined TodoSimple
to serve requests sent to http://127.0.0.1:5000/1
. I would recommend to change the code to sth like below
api.add_resource(TodoSimple, '/todo/<int:todo_id>')
and then, try to access it via GET http://127.0.0.1:5000/todo/1