noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 507 forks source link

How to change response content type in swagger UI? #480

Open LLCcom opened 6 years ago

LLCcom commented 6 years ago

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

kenneho commented 6 years ago

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

kenneho commented 6 years ago

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.

rob-smallshire commented 5 years ago

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
mateo2181 commented 4 years ago

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.