swagger-Unrecognized response type; displaying content as text

Viewed 10616

I built a swagger using swagger-ui-express module for my node js backend. I have an endpoint that returns files ( pdf-doc or xlsx). When i test my endpoint on swagger it rturns me 200 response with always this message on body 'Unrecognized response type; displaying content as text.' This is my swagger endpoint's code.

endpoint response image

 "/reports?typeDoc={typeDoc}&file={file}": {
            "get": {
                "tags": [
                    "Generate report"
                ],
                "description": "",
                "operationId": "",
                "parameters": [
                    {
                        "name": "typeDoc",
                        "in": "path",
                        "description": "type of document",
                        "required": true,
                        "type": "string"
                    },
                    {
                        "name": "file",
                        "in": "path",
                        "description": "name of the file",
                        "required": true,
                        "type": "string"
                    }
                ],
                "responses": {
                    "200":{
                    "description": "Report generated succefully. ",
                    "content":{
                        "application/octet-stream":{
                            "schema":{
                                "type": "string",
                                "format": "binary"}
                        }
                    }
                    },
                    "500": {
                        "description": "Error in zip file structure or typeDoc"
                    }
                }
            }   
    }



3 Answers

Try add headers like

content-disposition: attachment; filename = test.xlsx
content-type: multipart / form-data

and swagger-ui will understand the response type and offer download the file.

enter image description here

Swagger UI has an open issue for this. As a workaround add header ('filename' directive may be omitted)

content-disposition: attachment; filename="<some file name>"

Then Swagger UI will provide "Download file" link for the response body.

Note: content-type header should be set to the actual content type of the response (and it will be interpreted by browser while opening the download link); normally content-type of the response should not be 'multipart/form' as suggested by another answer.

You can try to use octet-stream as a fallback. header should look like this:

content-disposition: inline; filename=sample.pdf 
content-type: application/octet-stream
Related