8

看到这个问答,但没有与 OP 相同的结果如何以招摇的方式发布文件?

在我的 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 正确发布?

4

1 回答 1

0

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

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

于 2015-03-30T03:46:22.367 回答