flask-restplus
flask-restplus copied to clipboard
how to configure parameter content type
I am trying to configure multiple json types such as application/json and application/test+json. Basically is such an example where inputs are either xml or json or a custom mime-type implementation : http://petstore.swagger.io/#!/pet/addPet possible at all in flask-restplus? @noirbizarre any suggestions very much appreciated :)
Bump
From code, Flask-RESTplus Swagger generator seems to only support 'application/json', 'multipart/form-data', and 'application/x-www-form-urlencoded'. It is indeed desired that is supports manually specified content-types, e.g. 'text/plain' (in this case, it should be possible to document the parameter in SwaggerUI, but without imposing a validation scheme).
@marrrcin , @noirbizarre : maybe rename the issue to Should support manually specified content-types (or something like that)?
This is very much desired for us because we return large amounts of TSV-formatted data, no json, no quotes. Without this we cannot use flask-restplus
This is such a basic scenario, any update on this @noirbizarre ?
From the docs apply a response data transform to add your desired content type into the dropdown list, like so:
@api.representation('text/tsv')
def tsv_response(data, code, headers=None):
resp = app.make_response(data)
resp.headers['Content-Type'] = 'text/tsv'
return resp
It appears to work globally.
I want to set Accept header so that it's automatically showed in swagger UI. Currently, whenever I override default_mediatype
in Api object, all of the API Resource
s fall into this mediatype in Swagger.
Rather than override default_mediatype, use @api.representation(). This will cause your new type to be added to the dropdown menu on the swagger interface, so the user is able to select one type or the other per API.
I don't yet see a way to add it to only specific APIs. The type is added as an option for all APIs so they must support both formats, or at least give a message if the user selects a type that a particular API does not support. I'm still looking for a way to add the type to only certain APIs.
@api.representation()
only sets response type, not request type.
I guess request and response are all in the eye of the beholder:) @api.representation() sets the response type on the swagger UI, but in the request received by the server, the request accept type is set to the response type the user selects on the swagger UI. To decide the return type for the response from request received by the API, my code uses this check:
if request.headers['accept'] == 'text/tsv':
Yes, confusing, but give it a try.
Hi @marrrcin
I did a little implementation of this feature on my own fork.
For having the capability to add request type its necessary to modify the consume attribute on the swagger schema, and I added a decorator like the @api.representation('application/xml') in this case a called @api.consume('application/xml') to have this media type on the request media type available.
Check it out, maybe it could help.
I know you are looking for the parameter content type, but just incase someone is looking for the response content type this is a better solution in my opinion than using 'representation': https://github.com/noirbizarre/flask-restplus/issues/480
Hi,
Can we merge @isccarrasco code for "consume" decorator to flask-restx master release branch ?
As stated earlier its a very basic scenario that should be supported by flask-restx. When we use swagger ui, we expect flexibility to specify content-type before sending data to server.
any updates on this?