The Flask tutorial site here says that to create a RESTful API, you would write classes that extend restful.Resource
, then add them to the API by:
app = Flask(__name__)
api = restful.Api(app)
class HelloWorld(restful.Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
However, I've looked at quite a few tutorials that all just use functions with the @app.route('/path')
decorator that I'm more used to seeing in Flask apps. For example, here, they have:
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
And here:
@app.route('/')
def api_root():
return 'Welcome'
What's the difference between using the restful.Resource
class and just the decorated functions if any? If there are no differences, what should I be doing by convention to create a RESTful API?
Flask REST API Tutorial. REST API services let you interact with the database by simply doing HTTP requests. In this article you learn how to write a REST server using the Flask. This is often how the backend of web apps is created. Returning data is in JSON format and requests we are using are PUT, DELETE, POST, and GET.
Flask Restful is an extension for Flask that adds support for building REST APIs in Python using Flask as the back-end. It encourages best practices and is very easy to set up. Flask restful is very easy to pick up if you’re already familiar with flask. In flask_restful, the main building block is a resource.
"Flask allows Python developers to create lightweight RESTful APIs." Why Python? Why Flask? Why Python? Nowadays, choosing Python to develop applications is becoming a very popular choice.
RESTful APIs using JSON on the HTTP protocol is by far the most popular approach to creating web APIs. They enable developers to think in terms of resources with actions on those resources like CRUD (Create, Read, Update, Delete). However, there are upcoming APIs like GraphQL which is increasingly becoming popular with many applications.
Short answer:
restful.Resource is from a Flask-Restful extension, which is not Flask itself. Miguel's tutorial uses Flask to write a restful interface.
Long answer:
First of all, along with Flask, there are a number of Flask extensions. Although they work together, they are separate packages and are written by individual authors. Flask-Restful is an extension to Flask.
Miguel's tutorial explains how you can make a restful api using Flask by itself.
Flask-Restful with the aim to saving some of us from re-inventing the wheel, promises to turn a custom class(or a custom Python data structure) to a restful web service. Flask-RESTX, a fork of Flask-Restful, auto-generates api documentation with swagger UI.
In addition, Flask also documented the usage of MethodView to allow developers to write their own restful APIs. In parallel, Flask-Restless promises to turn a SqlAlchemy class into a restful web service.
An update(18/07/2016), flask-api turns a function/view into a restful interface and is designed by Tom Christie, the author of django restful framework.
an update(17/03/2021), Flask-RESTPlus does smiliar things as above libraries but it also helps you construct swagger API documentation, which is an extra bonus.
There are many roads to Roma.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With