flask_accepts icon indicating copy to clipboard operation
flask_accepts copied to clipboard

Multipart Request

Open umang-singhal opened this issue 5 years ago • 4 comments
trafficstars

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.

umang-singhal avatar Dec 11 '19 04:12 umang-singhal

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?

apryor6 avatar Dec 13 '19 00:12 apryor6

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?

umang-singhal avatar Dec 15 '19 15:12 umang-singhal

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.

apryor6 avatar Dec 21 '19 12:12 apryor6

Also be aware any PR will need associated test cases

apryor6 avatar Dec 21 '19 12:12 apryor6