# -*- coding: utf-8 -*-
# @Time : 2022/4/13 11:46 下午
# @Author : renwoxing
# @File : flask_restful_demo.py
# @Software: PyCharm
from flask import Flask, jsonify, abort, request
app = Flask(__name__)
books = [
{
'id': 1,
'name': u'大学',
'author': '张三',
'price': 98
},
{
'id': 2,
'name': u'骆驼祥子',
'author': '老舍',
'price': 98
}
]
@app.route("/bookstore/api/v1/books", methods=['GET'])
def get_books():
return jsonify({'books': books})
if __name__ == '__main__':
app.run(host="0.0.0.0", port="8000", debug=True)
current_app.response_class(
dumps(data, indent=indent, separators=separators) + "\n",
mimetype=current_app.config["JSONIFY_MIMETYPE"],
)
上面的dumps实际上是调用了itsdangerous 库json.dumps函数而已:
from itsdangerous import json as _json
_dump_arg_defaults(kwargs, app=app)
encoding = kwargs.pop("encoding", None)
rv = _json.dumps(obj, **kwargs)
if encoding is not None and isinstance(rv, text_type):
rv = rv.encode(encoding)
return rv