flask_accepts
flask_accepts copied to clipboard
Multipart Request
Currently, only content-type:json is supported in @accepts as per code here.
In my use-case, request schema has a file and other form parameters. Since flask keeps that data in request.values and request.form attributes it is currently not configurable.
My suggestion for this is to check the headers and parse the parameters accordingly. Please let me know If there is another way to get around it or this can be the right direction going forward. And If it is, I can pitch in to create a PR for it.
The way it is currently implemented, flask_accepts reserves the JSON body for the schema parameter if one has been provided. This is mostly to prevent logical conflict/collision between schemas and associated query parameters. This prevents the (potentially confusing) scenario where a query parameter is provided with the same name as an attribute in the schema.
How are you including a file inside of a Marshmallow schema? Or do you just mean that a file is part of the request, perhaps like in the example here?
Yes, exactly like in this example(the file is a part of a POST request) and the marshmallow schema consists of a fields.Raw attribute to parse that.
Since binary type object(file) cannot be JSON parsed, this request is sent as multipart and cannot be parsed using request.get_json(). Here we could use request.values and request.files to parse request data.
I have worked out a quick way-around here. Can you suggest if it is the right way or there can be a better way?
Yes this looks on track, just verify that request.files can be accessed safely even if it is not provided. I can’t recall the behavior and am on holiday unable to check for a couple of days. Provided that request.files returns an empty dict when not provided, your code looks fine.
Also be aware any PR will need associated test cases