Sending JSON data along with file
See original GitHub issueI’m trying to send a file with some metadata to a Spring Controller at the backend, but I’m not having much luck. I think I narrowed the problem down to the absence of a content-type to go along with the json-data.
The request send is something along the lines of…
------WebKitFormBoundarywdhA8TIFsWhbSoX9 Content-Disposition: form-data; name=“meta-data”
{“metadata”:{…}} ------WebKitFormBoundarywdhA8TIFsWhbSoX9 Content-Disposition: form-data; name=“file”; filename=“2014-03-invoice.pdf” Content-Type: application/pdf
In order for my Spring controller to recognize the meta-data as JSON (to map it to an object defined in my backend) the content type needs to be set to application/json, as shown in these docs… http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart-forms-non-browsers
I’ve tried using the formDataAppender but I’m either not using it right, or it isn’t possible to accomplish this with it.
My Spring controller (for completeness’ sake)
@RequestMapping(value = "/processwithfiles/", method = RequestMethod.POST)
public void processWithFiles(@RequestPart("meta-data") final Document document,
@RequestPart("file") final MultipartFile[] files, final HttpServletResponse response) {
...
}
This throws an HttpMediaTypeNotSupportedException “Content type ‘application/octet-stream’ not supported”
I’ve noticed that when I use the annotation @RequestParam instead, it works, but only if I change the type of the meta-data to String. That would require me to parse this string to a JSON object myself.
Is there a way make sure the request looks like the one in the example… http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart-forms-non-browsers
Issue Analytics
- State:
- Created 9 years ago
- Comments:15 (8 by maintainers)
You cannot set the type of individual parts of multipart form data request, so it will be received as a string and need to be parsed back to json. The cleaner way is to upload the file in one service call and return an id to the client then send that id along with metadata in a separate normal post call and then on the server relate the file and metadata using that id.
only blob can have type