0

I'm using Quart-Schema and have defined an endpoint for a multipart call with the help of https://pgjones.gitlab.io/quart-schema/how_to_guides/request_validation.html.

Looks like this:

@dataclass
class File:
    file: Optional[UploadFile] = None
    title: Optional[str] = None
    description: Optional[str] = None
    type: Optional[str] = None


@bp.post('/<int:entity_id>/files')
@validate_request(File, source=DataSource.FORM)
async def post_file(data: File, entity_id):
    """Add file for entity to database"""

The important bit is source=DataSource.FORM so it will pick up the form data instead of expecting JSON.

Works great! And this is how it looks in Swagger (auto-generated docs):

Swagger multipart call

It correctly shows all the fields and types and even includes an upload button. The only issue is, the request body type in Swagger is defined as application/x-www-urlencoded (only option in the drop-down). When I try out this request, it works when only sending plain field data, but when I add a file, it fails.

I can see no way to change this to multipart/form-data although I've seen Swagger examples showing this (though these examples don't use Quart-Schema, unfortunately).

It seems source=DataSource.FORM determines the request body type. Since there are only two options for source (the other one being DataSource.JSON) I see no way of getting Swagger to show/send multipart/form-data.

Is this a limitation of Quart-Schema, and if so: is there a solution for this?

kasimir
  • 1,506
  • 1
  • 20
  • 26

0 Answers0