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

时间:2013-11-21 17:02:08

标签: node.js curl express swagger swagger-ui

看到这个q& a但与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 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--

通过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正确发布?

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我可以使用普通的表单值进行POST,但是当我传递一个文件时,我没有数据。对我来说问题是由于使用Express 4,并没有安装和设置multer。详情请见:

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