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

How to change response content type in swagger UI?

Open LLCcom opened this issue 7 years ago • 5 comments

I have a [GET] route, which I wish to return a response with content-type="application/pdf". But looks like in swagger UI generated from flask-restplus we only have one response content type( which is json). Is there a way to change that in flask restplus and allow us to test that endpoint in swagger?

screenshot from 2018-06-21 11-41-28

LLCcom avatar Jun 21 '18 16:06 LLCcom

Did you find a solution to this? I have the same type of issue.

kenneho avatar Sep 06 '18 11:09 kenneho

I've found that using "@api.representation" (see https://flask-restful.readthedocs.io/en/0.3.5/extending.html#content-negotiation) add new entries to the response content type dropdown menu.

kenneho avatar Sep 12 '18 05:09 kenneho

The Swagger Response content type can be set with the produces decorator on a view method. Here's an example from my own code which sets the response content type to "image/png":

    @images_ns.response(HTTPStatus.NOT_FOUND, "Image content not found", problem_details_model)
    @images_ns.response(HTTPStatus.OK, "Image content found")
    @images_ns.produces(["image/png"])
    def get(self, id):
        """Returns the image binary."""
        image_meta = persistent.Image.query.get_or_404(id)
        image = extract_image_data(image_meta)
        response = make_response(image.as_bytes())
        response.headers.set("Content-Type", "image/png")
        return response

This gives:

Screenshot 2019-08-28 at 17 44 25

rob-smallshire avatar Aug 28 '19 15:08 rob-smallshire

Hi, I am trying to export a pdf file but when I add @api.produces(["application/pdf"]) I got error "AttributeError: 'Namespace' object has no attribute 'produces'".

flask-restplus==0.10.1

Do you know about this error? Thanks.

mateo2181 avatar Jun 24 '20 12:06 mateo2181