gpt4 book ai didi

node.js - Swagger 的 Node express : How to upload file from swagger-ui?

转载 作者:搜寻专家 更新时间:2023-10-31 22:25:54 24 4
gpt4 key购买 nike

看到这个问答但没有与 OP How to post files in 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 + '"}');

}
};

当我通过 curl 发布时,curl -v -F file=@scrot.png http://127.0.0.1:3000/uploads/file 一切都按预期工作。当我通过 swagger-ui (v 2.0.2) 发布时,它失败了。我在这两种情况下都使用了代理,并且 swagger-ui 没有指定内容类型,也没有传递数据。

通过 curl 简化原始帖子(使用上面的命令)

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--

通过 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

我应该如何配置我的路线/规范,以便 swagger-ui 正确发布?

最佳答案

我遇到了同样的问题,我可以用正常的表单值进行 POST,但是当我传递一个文件时,我没有得到任何数据。我的问题是由于使用 Express 4,并且没有安装和设置 multer。详情可以在这里找到:

https://github.com/swagger-api/swagger-node-express/issues/202

关于node.js - Swagger 的 Node express : How to upload file from swagger-ui?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20127338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com