Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File upload always sends 47 byte body #1416

Closed
MHova opened this issue Jun 25, 2015 · 8 comments
Closed

File upload always sends 47 byte body #1416

MHova opened this issue Jun 25, 2015 · 8 comments

Comments

@MHova
Copy link

MHova commented Jun 25, 2015

I'm trying to make use of the swagger-ui file upload feature. My Spring MVC endpoint is annotated as such:

    @RequestMapping(value = "/{id}", method = PUT, consumes = "*/*")
    @ApiOperation(
            value = "Upload a file",
            notes = "Some notes")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "file", required = true, dataType = "file", paramType = "body")})
    public ResponseEntity<?> putFile(@PathVariable final String id,
            @ApiIgnore @RequestBody final byte[] file) throws IOException {

        logger.info("Bytes {}", file.length);
        Files.write(file, new File("c:\\someFile.jpg"));

        return new ResponseEntity<Void>(HttpStatus.CREATED);
    }

swagger-ui correctly displays the file chooser, but when I select a file and click "Try It Out", the content size of the request sent to my server is always just 47 bytes. I am using Firefox 38.0.5 on Windows 7. Here's a screenshot of the request details captured by Firefox:

firefox

Am I doing something wrong?

@MHova
Copy link
Author

MHova commented Jun 25, 2015

I also noticed that swagger-ui sends the request as Content-Type:multipart/form-data even though this is a PUT endpoint. Is there a way to specify the request Content-Type in swagger-ui if the endpoint accepts */*?

@webron
Copy link
Contributor

webron commented Jul 5, 2015

Which version of swagger-ui do you use?

If you want to use file upload, you have to use the multipart/form-data or application/x-www-form-urlencoded regardless of the HTTP verb used.

@MHova
Copy link
Author

MHova commented Jul 6, 2015

Thanks for looking into this.

I'm using the latest Springfox swagger-ui, which uses swagger-ui 2.1.0 underneath the hood.

Let me phrase my question another way. How can one invoke a PUT file upload endpoint using swagger-ui? Is it currently possible? If so, how? If not, can support be added using XMLHttpRequest?

@MHova
Copy link
Author

MHova commented Jul 21, 2015

Any update on this?

@webron
Copy link
Contributor

webron commented Jul 22, 2015

I'd need to see the Swagger definition you get for the file upload.

@MHova
Copy link
Author

MHova commented Jul 22, 2015

Sorry about that. Here's the relevant portion of the swagger json.

"put": {

    "tags": 

[

    "documents"

],
"summary": "Create or update a hosted document",
"description": "An accompanying metadata resource at /metadata/{id} must already exist. Attempts to create/update an externally hosted document will be redirected.",
"operationId": "putDocumentUsingPUT",
"consumes": 
[

    "*/*"

],
"produces": 
[

    "*/*"

],
"parameters": 
[

{

    "name": "id",
    "in": "path",
    "description": "id",
    "required": true,
    "type": "string"

},
{

    "name": "Content-MD5",
    "in": "header",
    "description": "md5",
    "required": false,
    "type": "ref"

},
{

    "in": "body",
    "name": "document",
    "description": "document",
    "required": true,
    "schema": 

        {
            "$ref": "#/definitions/file"
        }
    }

],
"responses": 
{

    "200": 

{

    "description": "OK",
    "schema": 

    {
        "type": "string"
    }

},
"201": 
{

    "description": "Document created"

},
"204": 
{

    "description": "Document updated"

},
"400": 

        {
            "description": "Metadata must be created first|The Content-MD5 you specified did not match what we received."
        }
    }

},

@webron
Copy link
Contributor

webron commented Aug 3, 2015

Unfortunately, that's a partial definition and I can't test with it. That shouldn't give you the option to upload a file, and is not the way to describe file uploads using Swagger.

@fehguy
Copy link
Contributor

fehguy commented Dec 9, 2015

Please reopen if you're still having an issue. From looking at your spec file, you were using an incomplete swagger implementation or one with a bug.

@fehguy fehguy closed this as completed Dec 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants