Search code examples
pythonrestapiflash-message

Example of RESTful API in Flask Python


Can someone show me examples of making a RESTful API which uses database information in Flask? I have no idea how to implement POST, PUT and DELETE and I always get the 405 error where I can't use the method in url.


Solution

  • Have you add request method in your routing? you can following reference from: flask-restful

    from flask import Flask, request
    from flask_restful import Resource, Api
    
    app = Flask(__name__)
    api = Api(app)
    
    class TodoSimple(Resource):
        def get(self):
            # do get something
    
        def put(self):
            # do put something
    
        def delete(self):
            # do delete something
    
        def post(self):
            # do post something
    
    api.add_resource(TodoSimple, '/api/todo')
    
    if __name__ == '__main__':
        app.run(debug=True)