Another time saver in Flask is the jsonify() return, which wraps json.dumps() and turns the JSON output into a response object with application/json as the content type in the HTTP header. We can tweak the last script a bit, just like we will do in chapter9_5.py:
from flask import Flask, jsonifyapp = Flask(__name__)@app.route('/routers/<hostname>/interface/<int:interface_number>')def interface(hostname, interface_number): return jsonify(name=hostname, interface=interface_number)if __name__ == '__main__': app.run(host='0.0.0.0', debug=True)
We will see the result returned as a JSON object with the appropriate header:
$ http GET http://172.16.1.173:5000/routers/r1/interface/1HTTP/1.0 200 OKContent-Length: 36Content-Type: ...