2013-11-21 33 views
7

看到这个问答&一个,但没有相同的结果,OP How to post files in swagger?swagger-node-express:如何从swagger-ui上传文件?

在我招摇节点快车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 -v -F [email protected] 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-- 

通过招摇的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 

我应该如何配置我的路线/规范,这样招摇的UI将发布是否正确?

+0

你有没有解决过这个问题?碰到同样的事情。 – clay

+0

试着将''“paramType”设置为“form”而不是“body”。 – Ron

回答

0

我有这个相同的问题,我可以用普通的表单值做一个POST,但是当我传递一个文件时,我没有任何数据。这个问题对我来说是因为使用了Express 4,而没有安装和安装。详情可以在这里找到:

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