8

この 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 は content-type を指定しておらず、データを渡していません。

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 に答える