首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swagger-node-express:如何从swagger-ui上传文件?

swagger-node-express:如何从swagger-ui上传文件?
EN

Stack Overflow用户
提问于 2013-11-22 01:02:08
回答 1查看 4.7K关注 0票数 8

查看了此问答,但与OP How to post files in swagger?的结果不同

在我的swagger-node-express API中使用此规范

代码语言:javascript
复制
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 (v2.0.2)发布时,它失败了。我在这两种情况下都使用了代理,swagger-ui既没有指定content-type,也没有传递数据。

通过curl的缩写原始post (使用上面的命令)

代码语言:javascript
复制
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发布缩写的原始帖子

代码语言:javascript
复制
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能够正确开机自检?

EN

回答 1

Stack Overflow用户

发布于 2015-03-30 11:46:22

我也遇到过同样的问题,我可以用正常的表单值进行POST,但是当我传递一个文件时,我没有得到任何数据。对我来说,这个问题是由于使用Express4,而没有安装和设置multer。详细信息可在此处找到:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20127338

复制
相关文章

相似问题

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