我使用swagger-ui-express模块为我的node js后端构建了一个swagger。我有一个返回文件( pdf-doc或xlsx)的端点。当我在swagger上测试我的端点时,它返回200响应,并在body上始终显示此消息‘无法识别响应类型;将内容显示为文本。’这是我的swagger端点的代码。
"/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"
}
}
}
}
发布于 2020-06-16 05:43:26
Swagger UI为此提供了一个open issue。作为一种解决办法,添加标头(可以省略‘filename’指令)
content-disposition: attachment; filename="<some file name>"
然后Swagger UI将为响应正文提供“下载文件”链接。
注意: content-type header应设置为响应的实际内容类型(打开下载链接时,浏览器将对其进行解释);正常情况下,响应的content-type不应为另一个答案所建议的 'multipart/form‘。
发布于 2019-11-16 17:05:20
尝试像这样添加标题
content-disposition: attachment; filename = test.xlsx
content-type: multipart / form-data
swagger-ui将理解响应类型并提供下载文件。
发布于 2021-07-30 08:12:41
您可以尝试使用octet-stream
作为后备。报头应如下所示:
content-disposition: inline; filename=sample.pdf
content-type: application/octet-stream
https://stackoverflow.com/questions/56479965
复制相似问题