首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >swagger-无法识别的响应类型;将内容显示为文本

swagger-无法识别的响应类型;将内容显示为文本
EN

Stack Overflow用户
提问于 2019-06-06 22:44:18
回答 3查看 6.3K关注 0票数 5

我使用swagger-ui-express模块为我的node js后端构建了一个swagger。我有一个返回文件( pdf-doc或xlsx)的端点。当我在swagger上测试我的端点时,它返回200响应,并在body上始终显示此消息‘无法识别响应类型;将内容显示为文本。’这是我的swagger端点的代码。

代码语言:javascript
运行
复制
 "/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"
                    }
                }
            }   
    }
EN

回答 3

Stack Overflow用户

发布于 2020-06-16 05:43:26

Swagger UI为此提供了一个open issue。作为一种解决办法,添加标头(可以省略‘filename’指令)

代码语言:javascript
运行
复制
content-disposition: attachment; filename="<some file name>"

然后Swagger UI将为响应正文提供“下载文件”链接。

注意: content-type header应设置为响应的实际内容类型(打开下载链接时,浏览器将对其进行解释);正常情况下,响应的content-type不应为另一个答案所建议的 'multipart/form‘。

票数 8
EN

Stack Overflow用户

发布于 2019-11-16 17:05:20

尝试像这样添加标题

代码语言:javascript
运行
复制
content-disposition: attachment; filename = test.xlsx
content-type: multipart / form-data

swagger-ui将理解响应类型并提供下载文件。

票数 5
EN

Stack Overflow用户

发布于 2021-07-30 08:12:41

您可以尝试使用octet-stream作为后备。报头应如下所示:

代码语言:javascript
运行
复制
content-disposition: inline; filename=sample.pdf 
content-type: application/octet-stream
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56479965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档