2013-11-21 36 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