Skip to content

FileUpload not working with JSON data #40

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

Open
dheerajuppalapati opened this issue May 4, 2017 · 10 comments
Open

FileUpload not working with JSON data #40

dheerajuppalapati opened this issue May 4, 2017 · 10 comments
Labels

Comments

@dheerajuppalapati
Copy link

I tried the example https://github.com/springfox/springfox-demos/blob/be4118bb031c8dcded514c33d74cae52fc51ff08/boot-swagger/src/main/java/springfoxdemo/boot/swagger/web/FileUploadController.java

Just changed the @RequestPart String description to @RequestBody Component component which should be a JSON input.

Here is the code sample
@RequestMapping(value = "/v1/component/fileupload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Component fileComplaint(
@RequestBody Component component, @RequestPart MultipartFile file){
...........................
}

Here is what the swagger-ui looks like
image

Here is the request details
image
image

Here is the response details
image

Here are my questions:

  1. I'm not sure why does say image in swagger-ui. It should say "application/json" instead. How do i fix this?

  2. If you request at the request paramaters, the data i'm putting in the "component" parameter is not being sent to the back-end. It only sends the file details.

  3. How do i fix org.springframework.web.HttpMediaTypeNotSupportedException??

Any kind of help will be appreciated. I already checked out the suggestions in springfox/springfox#134 and it was of no help

Thanks,
Dheeraj

@dilipkrish
Copy link
Member

Swagger-ui does not support multipart file upload

@dheerajuppalapati
Copy link
Author

I'm guessing this is not the right place to ask questions about springfox?? Every-time I ask questions I get answers from you which do not make sense.

What does this mean "Swagger-ui does not support multipart file upload"?? Springfox is build on swagger and example you posted for sprinfox here https://github.com/springfox/springfox-demos/blob/be4118bb031c8dcded514c33d74cae52fc51ff08/boot-swagger/src/main/java/springfoxdemo/boot/swagger/web/FileUploadController.java is for Multipart file upload.

I asked a simple question and I keep getting ridiculous answer.

@NiteshMehta
Copy link

I am also facing similar problem with https://github.com/springfox/springfox-demos/blob/be4118bb031c8dcded514c33d74cae52fc51ff08/boot-swagger/src/main/java/springfoxdemo/boot/swagger/web/FileUploadController.java

When I modify @RequestPart String description to @RequestPart RequestObject requestObject

I am getting error
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported

Even the curl generated by swagger-ui produces same error, but once I add type=application/json to body problem is fixed
curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' -F 'requestObject=@/req.json;type=application/json' 'http://localhost:8080/MyService/api/upload'

looking at swagger-api/swagger-core#636, it looks like swagger-ui have support to provide content-type for RequestPart.

springfox version 2.7.0
springboot Version 1.5.4.RELEASE

@dilipkrish
Copy link
Member

Request parts, and multipart requests in general are not supported by swagger-ui. Atleast as of version 2.0 of the spec.

@NiteshMehta
Copy link

Hi Dilip,
I am confused here, isn't FileUploadController.java multipart request with two request parts ?

@dilipkrish
Copy link
Member

dilipkrish commented Jan 4, 2018

It is but swagger-ui and OpenAPI spec in general doesnt support multipart requests well. FileUploadController is just to demonstrate that springfox tries to generate the right description

@NiteshMehta
Copy link

Thanks Dilip
FileUploadController.java actually works, but its just that when we change @RequestPart String description to @RequestPart RequestObject requestObject it produces following error org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported

so does this means that it is partially supported?

@rajademo
Copy link

Dilip / Nitesh, any update on this issue.
I'm also facing the same issue. When i change @RequestPart String to @RequestPart Object, i'm getting the error.

@dilipkrish
Copy link
Member

Multipart requests are not supported very well in Swagger-ui unfortunately

@ahmeterdem
Copy link

"Multipart requests are not supported very well in Swagger-ui unfortunately"
then for file upload instead of multipart file which data type can we use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants