path (same with view_args, added in APIFlask 1.0.2)
cookies
headers
files
form_and_files
json_or_form
Tip
Cookie parameters don't work well with OpenAPI, see this issue for more details.
You can declare multiple input data with multiple app.input decorators. However,
you can only declare one body location for your view function, one of:
json
form
files
form_and_files
json_or_form
@app.get('/')@app.input(FooHeader,location='headers')# header@app.input(FooQuery,location='query')# query string@app.input(FooIn,location='json')# JSON data (body location)defhello(headers_data,query_data,json_data):pass
In the generated OpenAPI spec, the request body content type is set based on the input location you declared.
json: application/json
form: application/x-www-form-urlencoded
files: multipart/form-data
form_and_files: multipart/form-data
json_or_form: application/json and application/x-www-form-urlencoded. For this location, APIFlask will
try to parse the request body as JSON first, if it fails, it will try to parse it as form data. The OpenAPI spec
will show both content types, for example:
When you declared an input with app.input decorator, APIFlask (webargs) will get the data
from specified location and validate it against the schema definition.
If the parsing and validating success, the data will pass to the view function as keyword argument
named {location}_data:
Notice the argument name for URL variables (category, article_id) must match the variable name.
If validation failed, a 400 error response will be returned automatically. Like any other error response,
this error response will contain message and detail fields:
message
The value will be Validation error, you can change this via the config
VALIDATION_ERROR_DESCRIPTION.
detail
The detail field contains the validation information in the following format:
Here we use secure_filename to clean the filename, notice it will only keep ASCII characters.
You may want to create a random filename for the newly uploaded file, this
SO answer may be helpful.
The file object is an instance of werkzeug.datastructures.FileStorage, see more details
in Werkzeug's docs.
Use form_and_files location if you want to put both files
and other normal fields in one schema:
You can set request examples for OpenAPI spec with the example and examples
parameters, see this section in the
OpenAPI Generating chapter for more details.