rog*_*ter 7 curl node.js express swagger swagger-ui
看到这个q&a,但没有与OP相同的结果如何在swagger中发布文件?
在我的swagger-node-express API中使用此规范
exports.saveFile = {
'spec' : {
"description" : "Saves a file to filesystem",
"path" : "/uploads/file",
"notes" : "",
"summary" : "POST a file to storage",
"method" : "POST",
/* "supportedContentTypes" : [ 'multipart/form-data' ], */
"produces":[ "application/json" ],
"consumes":[ "multipart/form-data" ],
"params" : [{
"name": "File",
"description": "The file to upload.",
"paramType": "body",
"required": true,
"allowMultiple": false,
"dataType": "file"
}
],
"responseClass" : "ArbitraryJson",
"errorResponses" : [ errors.invalid('file') ],
"nickname" : "saveFile"
},
'action' : function(req, res) {
res.send('{"msg":"success", "file path": "' + req.files.file.path + '"}');
}
};
Run Code Online (Sandbox Code Playgroud)
当我通过curl POST时,curl -v -F file=@scrot.png http://127.0.0.1:3000/uploads/file一切都按预期工作.当我通过swagger-ui(v 2.0.2)发帖时,它失败了.我在两种情况下都使用了代理,并且swagger-ui没有指定内容类型,也没有传递数据.
通过卷曲缩写原始帖子(使用上面的命令)
POST http://127.0.0.1:3000/uploads/file HTTP/1.1
User-Agent: curl/7.27.0
Host: 127.0.0.1:3000
Accept: */*
Content-Length: 43947
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------9af70f8a272c
------------------------------9af70f8a272c
Content-Disposition: form-data; name="file"; filename="scrot.png"
Content-Type: application/octet-stream
...
------------------------------9af70f8a272c--
Run Code Online (Sandbox Code Playgroud)
通过swagger-ui缩写的原始帖子
POST http://127.0.0.1:3000/uploads/file HTTP/1.1
Host: 127.0.0.1:3000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0
Accept: application/json
Accept-Language: en-US,en;q=0.5
Referer: http://127.0.0.1:3000/docs/
Content-Length: 0
Content-Type: text/plain; charset=UTF-8
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Run Code Online (Sandbox Code Playgroud)
我应该如何配置我的路线/规格,以便swagger-ui正确发布?
我遇到了同样的问题,我可以使用正常的表单值执行 POST,但是当我传递文件时,我没有得到任何数据。对我来说,问题是由于使用 Express 4,并且没有安装和设置 multer。详细信息可以在这里找到:
https://github.com/swagger-api/swagger-node-express/issues/202
| 归档时间: |
|
| 查看次数: |
3669 次 |
| 最近记录: |