Since APIFlask is a thin wrapper on top of Flask, you only need to change very little
code to migrating your application to APIFlask (typically less than ten lines of code).
You only need to import APIFlask, APIBlueprint, and other utilities APIFlask
provides from the apiflask package. For others, you still import them from
the flask package:
You can use message and detail parameter to pass error message and detailed
information in the abort() function.
Warning
The function abort_json() was renamed to abort() in the
version 0.4.0.
JSON errors and mix the use of flask.abort() and apiflask.abort()¶
When you change the base application class to APIFlask, all the error responses
will automatically convert to JSON format even if you use Flask's abort() function:
Now you can still use abort from apiflask package to return a JSON error
response. To mix the use of flask.abort and apiflask.abort, you will need
to import one of them with a different name:
In APIFlask 1.x, to ensure the nature order of the arguments passed to the view
function, it passes path arguments to view functions as positional
arguments.
With APIFlask, you can accept the arguments in the view function in a natural way
(from left to right, from top to bottom):
defget_article(category,article_id,query,data)
This may causes issues when you use a custom decorator that access the arguments. So in
the APIFlask 2.x, all the arguments passed to the view function will be keyword arguments.
The argument passed by the app.input() decorator will be named {location}_data,
a custom argument name can be set with the arg_name argument. See more details in
Basic Usage.
For a simple view function without @app.output decorator, you can return a dict or
a list as JSON response. The returned dict and list will be converted to a JSON
response automatically (by calling
jsonify() underlay).
Tip
Although list looks like tuple, only list return values will be serialized to JSON
response. Tuple has
special meaning.
Starting from Flask 2.2, the list return values are supported natively.
When you added a @app.output decorator for your view function, APIFlask expects you to
return an ORM/ODM model object or a dict/list that matches the schema you passed in the
@app.output decorator. If you return a Response object, APIFlask will return it
directly without any process.