python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.15k stars 331 forks source link

Document/specify request parser PUT body that is not JSON #67

Open RinkeHoekstra opened 4 years ago

RinkeHoekstra commented 4 years ago

Question I'm using the request parser to specify & document an API that expects a (possibly) non-JSON payload in a PUT request, where the Content-Type header specifies the mime type of the body contents. My issue is with the Swagger spec that is generated from the parser arguments.

At the moment, the location parameter to add_argument can only be set to form or json. The first will have the Swagger UI PUT a POST form, instead of a vanilla body, and it sets the Content-Type to application/x-www-form-urlencoded or multipart/form-data in the Swagger API documentation. The second will force the Content-Type to application/json which is not correct either.

Is there a way to have the Swagger UI show that it expects a PUT body without forcing the content type?

Context Code looks something like this:

blueprint = Blueprint('api', __name__)
api = Api(blueprint)
ns = api.namespace('generate', description='Generator service')

# Register the api blueprint
app.register_blueprint(blueprint, url_prefix='/api')

generate_data_parser = api.parser()
generate_data_parser.add_argument('Content-Type', required=False, default="application/ld+json", help='The MIME type of of the data provided.', location='headers')
# Set location either to json or form, but cannot set to body?
generate_data_parser.add_argument("data", required=True, location="json", help="The data that you want to validate.")
@ns.route('/data', methods=['PUT'], doc={"name": "Generate something cool", "description": "Generate something cool"})
class GenerateData(Resource):

    @ns.expect(generate_data_parser)
    def put(self):
        byte_data = request.data
        mime_type = request.headers.get('Content-Type', "application/ld+json")

        # DO SOMETHING REALLY INTELLIGENT

        return make_response(...)
RinkeHoekstra commented 4 years ago

(FWIW, I know the request parser is set to be deprecated, but currently the rest of my code relies on it...)