flask-restplus icon indicating copy to clipboard operation
flask-restplus copied to clipboard

how to configure parameter content type

Open harishkashyap opened this issue 9 years ago • 13 comments

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 :)

harishkashyap avatar Jan 29 '16 17:01 harishkashyap

Bump

marrrcin avatar Mar 12 '18 14:03 marrrcin

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)?

aparamon avatar May 28 '18 13:05 aparamon

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

ucscHexmap avatar Nov 29 '18 21:11 ucscHexmap

This is such a basic scenario, any update on this @noirbizarre ?

marrrcin avatar Dec 04 '18 08:12 marrrcin

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.

terraswat avatar Dec 04 '18 09:12 terraswat

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 Resources fall into this mediatype in Swagger.

marrrcin avatar Dec 04 '18 09:12 marrrcin

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.

terraswat avatar Dec 04 '18 17:12 terraswat

@api.representation() only sets response type, not request type.

marrrcin avatar Dec 04 '18 19:12 marrrcin

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.

terraswat avatar Dec 04 '18 20:12 terraswat

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.

isccarrasco avatar Mar 27 '19 06:03 isccarrasco

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

Pollopiccante avatar Sep 07 '21 09:09 Pollopiccante

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.

Aash90 avatar Nov 12 '21 06:11 Aash90

any updates on this?

threeal avatar Aug 14 '23 15:08 threeal