Implementing API endpoints
The implementation of API endpoints uses the same bolts and knots applied in creating web-based components in Chapters 1 and 2, such as declaring path variables, accessing the request through the request
proxy object, returning the same Response
object, and using the same @route()
decorator. A GET API endpoint that returns a JSON response is as follows:
@current_app.route("/index", methods = ['GET']) def index(): response = make_response(jsonify(message='This is an Online Pizza Ordering System.', today=date.today()), 200) return response
This index()
function, found in the app/api/index.py
module, is exactly similar to the web-based view function, except that make_response()
requires the jsonify()
instead of the render_template()
method.
The jsonify()
is a Flask utility method that serializes any data to produce an application/json
response. It converts multiple values into an array...