9

Saw this q&a but did not have same results as OP How to post files in swagger?

Using this spec in my 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 + '"}');

  }
};

When I POST via curl, curl -v -F [email protected] http://127.0.0.1:3000/uploads/file everything works as expected. When I post via swagger-ui (v 2.0.2) it fails. I used a proxy in both situations and the swagger-ui isn't specifying the content-type nor is it passing the data.

Abbreviated raw post via curl (using command above)

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--

Abbreviated raw post via 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

How should I configure my route/spec so the swagger-ui will post correctly?

2
  • Did you ever solve this? Running into the same thing.
    – clay
    Commented Mar 10, 2014 at 20:55
  • Try setting the "paramType" to "form" rather than "body".
    – Ron
    Commented Dec 31, 2014 at 12:53

1 Answer 1

0

I had this same issue, where I could do a POST with normal form values, but when I passed a file, I got no data. The issue for me was due to using Express 4, and not having multer installed and setup. Details can be found here:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.