Reputation: 17372
I'm trying to upload a file using Swagger and Flask. I've the following configuration for swagger.
"/user/register/": {
"post": {
"tags": ["user"],
"summary": "Register a new user",
"description": "",
"operationId": "registerUser",
"consumes": ["application/json"],
"produces": ["application/json"],
"parameters": [{
"in": "body",
"name": "body",
"description": "User object that needs to be added.",
"required": true,
"schema": {
"$ref": "#/definitions/User"
}
},
{
"name": "file",
"in": "path",
"description": "file to upload",
"required": true,
"type": "file"
}]
}
},
I do get the option for uploading a file, but when I try to receive it at the backend,(using print request.files
) it returns me nothing.
How can I receive the file (selected at swagger level) at the backend.??
Upvotes: 4
Views: 7218
Reputation: 2825
iFile = request.files.getlist('file')[0]
- This command reads the file uploaded via swagger UI.
Upvotes: 2
Reputation: 10807
For file
, the type should be formData
instead of path
. See below for an example
{
"name": "file",
"in": "formData",
"description": "file to upload",
"required": false,
"type": "file"
}
For consumes
, it should be multipart/form-data
Upvotes: 1